Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 29, 2019 22:18
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/cf4654c1a1bba3a1686c75acac6769ce to your computer and use it in GitHub Desktop.
Save parzibyte/cf4654c1a1bba3a1686c75acac6769ce to your computer and use it in GitHub Desktop.
/*
Ejemplo 3: parámetros de la url
@author parzibyte
*/
package main
import (
"fmt" // Imprimir en consola
"io" // Ayuda a escribir en la respuesta
"log" //Loguear si algo sale mal
"net/http" // El paquete HTTP
)
func main() {
http.HandleFunc("/saludo", func(w http.ResponseWriter, peticion *http.Request) {
listaDeNombres, existen := peticion.URL.Query()["nombre"]
// Imprimir para depurar
fmt.Printf("%v\n", listaDeNombres)
if existen {
io.WriteString(w, "Hola, "+listaDeNombres[0])
} else {
io.WriteString(w, "Hola, desconocido")
}
})
direccion := ":8080" // Como cadena, no como entero; porque representa una dirección
fmt.Println("Servidor listo escuchando en " + direccion)
log.Fatal(http.ListenAndServe(direccion, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment