github.com/giovannyortegon/go@v0.0.0-20220115155912-8890063f5bdd/src/WebProfesional/1-Template/WebTemplate.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"text/template"
     8  )
     9  
    10  type Users 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 Index(rw http.ResponseWriter, r *http.Request) {
    23  	fmt.Println("Funcion Index ")
    24  	c1 := Curso{"Java"}
    25  	c2 := Curso{".Net"}
    26  	c3 := Curso{"Python"}
    27  	c4 := Curso{"Go"}
    28  
    29  	cursos := []Curso{c1, c2, c3, c4}
    30  	tmp, err := template.ParseFiles("index.html")
    31  
    32  	user := Users{"Giovanny", 36, true, true, cursos}
    33  
    34  	if err != nil {
    35  		panic(err)
    36  	} else {
    37  		tmp.Execute(rw, user)
    38  	}
    39  }
    40  
    41  func main() {
    42  	mux := http.NewServeMux()
    43  	mux.HandleFunc("/", Index)
    44  
    45  	fmt.Println("El servidor esta corriendo en el puerto 1234")
    46  	fmt.Println("Run server: http://localhost:1234/")
    47  	server := &http.Server{
    48  		Addr:    "localhost:1234",
    49  		Handler: mux,
    50  	}
    51  	log.Fatal(server.ListenAndServe())
    52  
    53  }