Herramientas de usuario

Herramientas del sitio


wiki2:go_useful

¡Esta es una revisión vieja del documento!


Useful Go

Installing

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

Projects

dep tool for managing dependences

To install: apt install go-dep. To initialize a project:

$ export GOPATH=`pwd`
$ mkdir src             
$ cd src 
$ mkdir marcarrodes
$ cd marcarrodes 
$ dep init
  • The Gopkg.toml file is where you specify your dependencies and the particular versions of these dependencies that you wish your project to use.
  • The Gopkg.lock file is a transitively complete snapshot of your project’s dependency graph.
  • The vendor/ directory is where your dependencies are stored. It’s the equivalent to the node_modules/

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

Project layout

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.
  • build
  • deployments
  • test
  • doc
  • tools Supporting tools for this project.
  • examples
  • third_party
  • githooks
  • assets
  • website

You do not have to have a src directory.


Gotchas

Program

Imports:

import "os"
argumentsWithoutProgramName = os.Args[1:]

Arrays (or, in Go: Slices)

len(arr)
wiki2/go_useful.1566297671.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)