Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active May 30, 2019 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/a1f88189652e5e090b63ee2972e37fa3 to your computer and use it in GitHub Desktop.
Save parzibyte/a1f88189652e5e090b63ee2972e37fa3 to your computer and use it in GitHub Desktop.
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