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/17 19:12] alfred [Basic library] |
wiki2:go_basics [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 32: | Línea 32: | ||
| ''godoc fmt'' \\ | ''godoc fmt'' \\ | ||
| + | ''godoc fmt Println'' \\ | ||
| If you want to get documentation for the fmt package, type the following command in | If you want to get documentation for the fmt package, type the following command in | ||
| the terminal. The godoc tool also provides browsable documentation on a web interface. To access the documentation through a web-based interface, start the web server provided by the godoc tool. Type the following command in the terminal: \\ | the terminal. The godoc tool also provides browsable documentation on a web interface. To access the documentation through a web-based interface, start the web server provided by the godoc tool. Type the following command in the terminal: \\ | ||
| Línea 76: | Línea 77: | ||
| f := "short" | f := "short" | ||
| </code> | </code> | ||
| + | Go also has another shorthand when you need to define multiple variables: | ||
| + | <code> | ||
| + | var ( | ||
| + | a = 5 | ||
| + | b = 10 | ||
| + | c = 15 | ||
| + | ) | ||
| + | </code> | ||
| + | Use the keyword var (or const ) followed by parentheses with each variable on its own line. | ||
| ==== Constantes ==== | ==== Constantes ==== | ||
| <code> | <code> | ||
| Línea 151: | Línea 160: | ||
| x := [5]int{2: 10, 4: 40} // [0 0 10 0 40] | x := [5]int{2: 10, 4: 40} // [0 0 10 0 40] | ||
| </code> | </code> | ||
| + | |||
| + | For example, while ''arr[0:5]'' returns [1,2,3,4,5] , ''arr[1:4]'' returns [2,3,4] . ''arr[0:]'' is the same as ''arr[0:len(arr)]'' , ''arr[:5]'' is the same as ''arr[0:5]'', and ''arr[:]'' is the same as ''arr[0:len(arr)]'' . | ||
| + | |||
| === Slices === | === Slices === | ||
| Línea 508: | Línea 520: | ||
| </code> | </code> | ||
| + | === Pointers === | ||
| + | |||
| + | <code> | ||
| + | func one(xPtr *int) { | ||
| + | *xPtr = 1 | ||
| + | } | ||
| + | func main() { | ||
| + | xPtr := new(int) | ||
| + | one(xPtr) | ||
| + | fmt.Println(*xPtr) // x is 1 | ||
| + | } | ||
| + | </code> | ||
| ==== Interfaces ==== | ==== Interfaces ==== | ||
| Línea 669: | 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 685: | Línea 712: | ||
| sleep := rand.Int63n(1000) | sleep := rand.Int63n(1000) | ||
| </code> | </code> | ||
| + | |||
| + | ===== Notes ===== | ||
| + | ==== Install last version ==== | ||
| + | |||
| + | <code> | ||
| + | wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz | ||
| + | sudo tar -xvf go1.9.2.linux-amd64.tar.gz | ||
| + | sudo mv go /usr/local | ||
| + | export GOROOT=/usr/local/go | ||
| + | sudo ln -s /usr/local/go/bin/go /usr/bin/go | ||
| + | sudo ln -s /usr/local/go/bin/godoc /usr/bin/godoc | ||
| + | sudo ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt | ||
| + | </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]] | ||
| + | |||