Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:go_basics [2017/12/26 12:38] alfred [Rand] |
wiki2:go_basics [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 693: | Línea 693: | ||
| channel until its capacity of two. Because this channel is buffered, values can be sent without depending | channel until its capacity of two. Because this channel is buffered, values can be sent without depending | ||
| on a receiver receiving them. If you try to add more values than its capacity, you will get an error. Once the values are sent into the buffered channel, they can be received from the channel. | on a receiver receiving them. If you try to add more values than its capacity, you will get an error. Once the values are sent into the buffered channel, they can be received from the channel. | ||
| + | |||
| + | ===== No so basic ===== | ||
| + | ==== Annotations (or struct tags) ==== | ||
| ===== Standard library ===== | ===== Standard library ===== | ||
| Línea 710: | Línea 713: | ||
| </code> | </code> | ||
| - | ==== Notes ==== | + | ===== Notes ===== |
| - | === Install last version === | + | ==== Install last version ==== |
| <code> | <code> | ||
| Línea 722: | Línea 725: | ||
| sudo ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt | sudo ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt | ||
| </code> | </code> | ||
| + | |||
| + | ==== Config VS Code to develop in Go ==== | ||
| + | You will need to install the 'golang' package. Also, when it is enabled, let it install the other tools. | ||
| + | |||
| + | If you wanted to debug you should change into the configuration the next variable (specially if only had a main.go file): | ||
| + | <code> | ||
| + | "program": "${workspaceRoot}", | ||
| + | </code> | ||
| + | A full example is: | ||
| + | <code> | ||
| + | { | ||
| + | // Use IntelliSense to learn about possible attributes. | ||
| + | // Hover to view descriptions of existing attributes. | ||
| + | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| + | "version": "0.2.0", | ||
| + | "configurations": [ | ||
| + | { | ||
| + | "name": "Launch", | ||
| + | "type": "go", | ||
| + | "request": "launch", | ||
| + | "mode": "auto", | ||
| + | "program": "${workspaceRoot}", | ||
| + | "env": {}, | ||
| + | "args": [], | ||
| + | "showLog": true | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | </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]] | ||
| + | |||