-
-
Save parzibyte/a1f88189652e5e090b63ee2972e37fa3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enrutador.HandleFunc("/usuarios", obtenerUsuarios).Methods("GET") | |
func obtenerUsuarios(respuesta http.ResponseWriter, peticion *http.Request) { | |
// Nota: estos usuarios podrían venir de una base de datos que podrían obtenerse dentro | |
// de cada ruta | |
type Usuario struct { | |
Id int `json:"id"` | |
Correo string `json:"correo"` | |
} | |
var usuarios []Usuario = []Usuario{ | |
Usuario{ | |
Id: 1, | |
Correo: "contacto@parzibyte.me", | |
}, | |
Usuario{ | |
Id: 2, | |
Correo: "john.galt@atlas.com", | |
}, | |
} | |
// También podrías codificar otro tipo de datos como un arreglo plano | |
// o una simple variable, todo lo soportado por JSON: | |
// https://parzibyte.me/blog/2019/05/16/codificar-decodificar-json-go-golang/ | |
json.NewEncoder(respuesta).Encode(usuarios) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment