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_useful [2019/08/20 10:45] alfred [Program] |
wiki2:go_useful [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 19: | Línea 19: | ||
| - | ==== dep tool for managing dependences ==== | + | ==== go mod for managing dependences ==== |
| + | After Go 1.11. | ||
| + | <code> | ||
| + | go mod init <project name> | ||
| + | # Do not do it in the GOPATH | ||
| + | </code> | ||
| + | |||
| + | When you do ''go build'' it downloads dependencies automatically. | ||
| + | |||
| + | There will appear ''go.mod'' y ''go.sum'' files. The first one to list dependences, the second one the checksums. | ||
| + | |||
| + | <code> | ||
| + | go mod tidy # To remove non-used libraries | ||
| + | go mod vendor # To vendor dependencies in vendor directory. | ||
| + | </code> | ||
| + | |||
| + | Then, if you added a new package to your project, to use them: | ||
| + | <code> | ||
| + | import "project/package" | ||
| + | </code> | ||
| + | ==== dep tool for managing dependences (DEPRECATED) ==== | ||
| To install: ''apt install go-dep''. To initialize a project: | To install: ''apt install go-dep''. To initialize a project: | ||
| Línea 101: | Línea 121: | ||
| ====== Gotchas ====== | ====== Gotchas ====== | ||
| + | ===== Basic ===== | ||
| + | <code> | ||
| + | // Define a string array variable | ||
| + | var ( | ||
| + | viewNames = []string{"projects", "generations", "info", "menu"} | ||
| + | ) | ||
| + | |||
| + | |||
| + | // Loop over an slice | ||
| + | for _, v := range viewNames { | ||
| + | fmt.Println(v) | ||
| + | } | ||
| + | |||
| + | // Get a variable without using it | ||
| + | _, err := g.SetCurrentView(name) | ||
| + | </code> | ||
| + | ===== Conversions ===== | ||
| + | Previous code: | ||
| + | <code> | ||
| + | import "strconv" | ||
| + | </code> | ||
| + | Code | ||
| + | <code> | ||
| + | // String to byte[] | ||
| + | buff := []byte("Here is a string....") | ||
| + | |||
| + | // Byte to String | ||
| + | str := string(buff) | ||
| + | |||
| + | // Int to string | ||
| + | str := strconv.Itoa(currentNumber) | ||
| + | </code> | ||
| ===== Program ===== | ===== Program ===== | ||
| Previous code used: | Previous code used: | ||
| Línea 109: | Línea 161: | ||
| <code> | <code> | ||
| argumentsWithoutProgramName = os.Args[1:] | argumentsWithoutProgramName = os.Args[1:] | ||
| + | settings_path = os.Getenv("GLOUDVIEWER_CONFIG") | ||
| </code> | </code> | ||
| + | ===== Arrays (or, in Go: Slices) ===== | ||
| + | |||
| + | <code> | ||
| + | len(arr) | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Maps ===== | ||
| + | |||
| + | <code> | ||
| + | var m map[string]Vertex | ||
| + | m = make(map[string]Vertex) | ||
| + | m["Bell Labs"] = Vertex{40.68433, -74.39967} | ||
| + | </code> | ||
| + | <code> | ||
| + | keys := make([]keyType, 0, len(myMap)) | ||
| + | values := make([]valueType, 0, len(myMap)) | ||
| + | for k, v := range myMap { | ||
| + | keys = append(keys, k) | ||
| + | values = append(values, v) | ||
| + | } | ||
| + | </code> | ||
| ===== Go Routines ===== | ===== Go Routines ===== | ||
| Previous code used: | Previous code used: | ||
| Línea 130: | Línea 205: | ||
| }("going") | }("going") | ||
| </code> | </code> | ||
| - | ===== Arrays (or, in Go: Slices) ===== | ||
| + | ===== Not so common types ===== | ||
| + | Previous code: | ||
| <code> | <code> | ||
| - | len(arr) | + | import "bytes" |
| + | </code> | ||
| + | |||
| + | Code: | ||
| + | <code> | ||
| + | // String buffer | ||
| + | buf := bytes.NewBufferString("") | ||
| + | fmt.Fprintln(buf, "Hello!") | ||
| + | str := buf.String() | ||
| + | </code> | ||
| + | |||
| + | ===== JSON ===== | ||
| + | Previous code used: | ||
| + | <code> | ||
| + | import "io/ioutil" | ||
| + | import "encoding/json" | ||
| + | |||
| + | type Generation struct { | ||
| + | Project string | ||
| + | Author string `json:"author"` | ||
| + | Version string `json:"version"` | ||
| + | } | ||
| + | </code> | ||
| + | Code: | ||
| + | <code> | ||
| + | // JSON file to struct object | ||
| + | b, err := ioutil.ReadFile(path) | ||
| + | if err != nil { | ||
| + | panic(err) | ||
| + | } | ||
| + | generation := Generation{} | ||
| + | json.Unmarshal(b, &generation) | ||
| + | </code> | ||
| + | |||
| + | ===== Filesystem ===== | ||
| + | Previous code: | ||
| + | <code> | ||
| + | import "strings" | ||
| + | import "path" | ||
| + | import "path/filepath" | ||
| + | </code> | ||
| + | |||
| + | Code: | ||
| + | <code> | ||
| + | // info is info os.FileInfo type | ||
| + | |||
| + | // Is a directory: | ||
| + | info.IsDir() | ||
| + | |||
| + | // Get parent path from a path (pth) | ||
| + | filepath.Dir(pth) | ||
| + | |||
| + | // File name without extension: | ||
| + | strings.TrimSuffix(info.Name(), pth.Ext(info.Name()) | ||
| + | |||
| + | // Join path | ||
| + | path.Join(arguments[0], selectedProject) | ||
| + | </code> | ||
| + | |||
| + | ====== Snippets ====== | ||
| + | ===== Map, reduce, filter in Go ===== | ||
| + | |||
| + | <code> | ||
| + | func mapXtoY(collection []X, fn func(elem X) Y) []Y { | ||
| + | var result []Y | ||
| + | for _, item := range collection { | ||
| + | result = append(result, fn(item)) | ||
| + | } | ||
| + | return result | ||
| + | } | ||
| + | |||
| + | func reduceXtoY(collection []X, init Y, fn func(memo Y, elem X) Y) Y { | ||
| + | result := init | ||
| + | for _, item := range collection { | ||
| + | result = fn(result, item) | ||
| + | } | ||
| + | return result | ||
| + | } | ||
| + | |||
| + | func filterXs(collection []X, fn func(elem X) bool) []X { | ||
| + | var result []X | ||
| + | for _, item := range collection { | ||
| + | if fn(item) { | ||
| + | result = append(result, item) | ||
| + | } | ||
| + | } | ||
| + | return result | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ===== Nested functions ===== | ||
| + | <code> | ||
| + | func f() { | ||
| + | foo := func(s string) { | ||
| + | fmt.Println(s) | ||
| + | } | ||
| + | |||
| + | foo("Hello World!") | ||
| + | } | ||
| </code> | </code> | ||