Herramientas de usuario

Herramientas del sitio


wiki2:go_basics

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:go_basics [2019/02/09 16:06]
alfred [Notes]
wiki2:go_basics [2020/05/09 09:25] (actual)
Línea 754: Línea 754:
 } }
 </​code>​ </​code>​
 +
 +
 +====== Go Web programming ======
 +===== Basics =====
 +==== Http server ====
 +<​code>​
 +package main
 +import (
 + "​net/​http"​
 +)
 +func main() {
 + mux := http.NewServeMux()
 + fs := http.FileServer(http.Dir("​public"​))
 + mux.Handle("/",​ fs)
 + http.ListenAndServe(":​8080",​ mux)
 +}
 +</​code>​
 +
 +
 +
 +====== Go Libraries ======
 +===== GORM =====
 +  * ''​go get -u github.com/​jinzhu/​gorm''​
 +  * ''​go get -u github.com/​go-sql-driver/​mysql''​
 +
 +<​code>​
 +package main
 +
 +import (
 + "​github.com/​jinzhu/​gorm"​
 + _ "​github.com/​jinzhu/​gorm/​dialects/​mysql"​
 + "​fmt"​
 +)
 +
 +type Person struct {
 + ID uint `json:"​id""​`
 + FirstName string `json:"​firstname"​`
 + LastName string `json:"​lastname""​`
 +}
 +
 +func main() {
 + db, err := gorm.Open("​mysql",​ "​root:​pass@/​test?​charset=utf8&​parseTime=True&​loc=Local"​)
 + if err != nil {
 +     fmt.Println(err)
 + }
 + defer db.Close()
 +
 + db.AutoMigrate(&​Person{})
 +
 + p1 := Person{FirstName:​ "​John",​ LastName: "​Doe"​}
 + p2 := Person{FirstName:​ "​Jane",​ LastName: "​Smith"​}
 +
 + db.Create(&​p1)
 + db.Create(&​p2)
 +
 + var p3 Person
 + db.First(&​p3)
 +
 + fmt.Println(p1.LastName)
 + fmt.Println(p2.LastName)
 + fmt.Println(p3.LastName)
 +}
 +</​code>​
 +
 +===== Gin =====
 +  * ''​go get github.com/​gin-gonic/​gin''​
 +<​code>​
 +package main
 +
 +import (
 + "​github.com/​gin-gonic/​gin"​
 + "​fmt"​
 +)
 +
 +func main() {
 + r := gin.Default()
 + r.GET("/",​ func(c *gin.Context) {
 + c.JSON(200,​ gin.H{
 + "​message":​ "Hello World",​
 + })
 + })
 + r.Run()
 +}
 +</​code>​
 +
 +==== Examble GORM + GIN ====
 +  * [[go_lib:​examples:​gormgin]]
 +
wiki2/go_basics.1549728378.txt.gz · Última modificación: 2020/05/09 09:25 (editor externo)