github.com/giovannyortegon/go@v0.0.0-20220115155912-8890063f5bdd/src/WebProfesional/2-Funciones/wego2.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"text/template"
     8  )
     9  
    10  type User struct {
    11  	UserName string
    12  	Edad     int
    13  	Activo   bool
    14  	Admin    bool
    15  	Cursos   []Curso
    16  }
    17  
    18  type Curso struct {
    19  	Nombre string
    20  }
    21  
    22  func Saludar(nombre string) string {
    23  	return "Hola " + nombre + " desde una Funcion."
    24  }
    25  
    26  func Index(rw http.ResponseWriter, r *http.Request) {
    27  	fmt.Println("In Index")
    28  
    29  	funciones := template.FuncMap{
    30  		"saludar": Saludar,
    31  	}
    32  
    33  	tmp, err := template.New("index.html").Funcs(funciones).ParseFiles("index.html")
    34  
    35  	if err != nil {
    36  		panic(err)
    37  	} else {
    38  		tmp.Execute(rw, nil)
    39  	}
    40  }
    41  
    42  func main() {
    43  	mux := http.NewServeMux()
    44  
    45  	mux.HandleFunc("/", Index)
    46  
    47  	fmt.Println("El servidor esta corriendo en el puerto 4040")
    48  	fmt.Println("Run Server: http://localhost:4040")
    49  	server := &http.Server{
    50  		Addr:    "localhost:4040",
    51  		Handler: mux,
    52  	}
    53  	log.Fatal(server.ListenAndServe())
    54  
    55  }