Programming
Go - tutorial day 1
Installation, follow your OS instructions.
In Fedora, can’t be simpler:
GOPATH, Go Workspace and Code Organization
Go requires an specific way to organize the code.
By convention, all your code and the imported code should live in a single workspace. The workspace is just a directory structure on your filesystem pointed by GOPATH environment variable.
In our examples:
Where:
- bin: contains executables binaries.
- Go builds and installs binaries executables in this directory.
- All Go programs meant to be executables muts have in the source code an special package called main main which defines the entry point of the program in a function called main().
- src: contains the Go source files.
src directory tipically contains several source code repos under version control systems with one or more Go packages.
- Every single source code Go file belongs to a package.
- Usually new subdirectories are created in the repo for each Go package.
- pkg: contains Go package files (.a).
- All the shared libraries, non executables, are stored en this directory.
- These libraries (packages) cant be executed directly) since those are no binaries. Usually imported and used by other executables packages.
Hello World
Subdirectory
Source code
src/hola/hola.go
Running you app
Building the binary
go run compiles and execute but do not produces a binary. To produce a binary use go build:
Building and installation in bin
Simillar to go build but result gets installed in bin.