¡Esta es una revisión vieja del documento!
Go to https://golang.org/dl/ and look for your system:
wget your_binary sudo tar -xvf <> sudo mv go /usr/local/ export GOROOT=/usr/local/go export GOPATH=$HOME/gopath export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
To check:
go version go env
To install: apt install go-dep. To initialize a project:
$ export GOPATH=`pwd` $ mkdir src $ cd src $ mkdir marcarrodes $ cd marcarrodes $ dep init
It's commands:
init Sets up a new Go project.status Reports the status of a project’s dependencies.ensure Ensures a dependency is safely vendored in the project.prune Prunes your dependencies, this is also done automatically by ensure.version Shows the dep version information.Add a dependence:
$ dep ensure -add github.com/foo/bar github.com/another/project ...
Update dependence:
// dry run testing an update $ dep ensure -update -n // non-dry run $ dep ensure -update // updates a specific package $ dep ensure -update github.com/gorilla/mux // updates to a specific version $ dep ensure -update github.com/gorilla/mux@1.0.0
Folders you could have:
cmd Main applications for this project. The directory name for each application should match the name of the executable you want to have (e.g., /cmd/myapp). Don't put a lot of code in the application directory. If you think the code can be imported and used in other projects, then it should live in the /pkg directory. If the code is not reusable or if you don't want others to reuse it, put that code in the /internal directory. It's common to have a small main function.internal Private application and library code. This is the code you don't want others importing in their applications or libraries. Put your actual application code in the /internal/app directory (e.g., /internal/app/myapp) and the code shared by those apps in the /internal/pkg directory (e.g., /internal/pkg/myprivlib).pkg Library code that's ok to use by external applications (e.g., /pkg/mypubliclib). Other projects will import these libraries expecting them to work, so think twice before you put something here.vendor Application dependencies.Others:
api OpenAPI/Swagger specs, JSON schema files, protocol definition files.web Web application specific components: static web assets, server side templates and SPAs.configs Configuration file templates or default configs.init System init (systemd, upstart, sysv) and process manager/supervisor (runit, supervisord) configs.scripts Scripts to perform various build, install, analysis, etc operations.builddeploymentstestdoctools Supporting tools for this project. examplesthird_partygithooksassetswebsite
You do not have to have a src directory.
Previous code used:
import "os"
Code:
argumentsWithoutProgramName = os.Args[1:]
len(arr)
Previous code used:
func f(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}
Code:
// Call a function as a goroutine
go f("goroutine")
// Call a function as goroutine at the same time we define it
go func(msg string) {
fmt.Println(msg)
}("going")