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 13:03] alfred [Conversions] |
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 141: | Línea 161: | ||
| <code> | <code> | ||
| argumentsWithoutProgramName = os.Args[1:] | argumentsWithoutProgramName = os.Args[1:] | ||
| + | settings_path = os.Getenv("GLOUDVIEWER_CONFIG") | ||
| </code> | </code> | ||
| Línea 149: | Línea 170: | ||
| </code> | </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 229: | Línea 266: | ||
| // Join path | // Join path | ||
| path.Join(arguments[0], selectedProject) | 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> | </code> | ||
| + | |||
| + | ===== Nested functions ===== | ||
| + | <code> | ||
| + | func f() { | ||
| + | foo := func(s string) { | ||
| + | fmt.Println(s) | ||
| + | } | ||
| + | |||
| + | foo("Hello World!") | ||
| + | } | ||
| + | </code> | ||
| + | |||