Monday, May 6, 2019

Golang - first test project

HOWTO structure Golang project

https://golang.org/doc/code.html

Settings GOPATH
https://github.com/golang/go/wiki/SettingGOPATH

Golang - complex project structure
https://github.com/golang-standards/project-layout

Directories You Shouldn't Have

/src

Some Go projects do have a src folder, but it usually happens when the devs came from the Java world where it's a common pattern. If you can help yourself try not to adopt this Java pattern. You really don't want your Go code or Go projects to look like Java :-)
Don't confuse the project level /src directory with the /src directory Go uses for its workspaces as described in How to Write Go Code. The $GOPATH environment variable points to your (current) workspace (by default it points to $HOME/go on non-windows systems). This workspace includes the top level /pkg, /bin and /src directories. Your actual project ends up being a sub-directory under /src, so if you have the /src directory in your project the project path will look like this: /some/path/to/workspace/src/your_project/src/your_code.go. Note that with Go 1.11 it's possible to have your project outside of your GOPATH, but it still doesn't mean it's a good idea to use this layout pattern.


Set environment
vi ~/.bash_profile

export GOPATH=$(go env GOPATH)
export PATH=$PATH:$(go env GOPATH)/bin

source ~/.bash_profile

Create first project
mkdir-p $GOPATH/src/github.com/dveselka/golang-hello


Install project

go install github.com/dveselka/golang-hello

[dave@localhost golang-hello]$ find  $GOPATH/src/github.com
/home/dave/go/src/github.com
/home/dave/go/src/github.com/dveselka
/home/dave/go/src/github.com/dveselka/golang-hello
/home/dave/go/src/github.com/dveselka/golang-hello/hello.go


Run project

[dave@localhost golang-hello]$ $GOPATH/bin/golang-hello 
Hello, world.

GitHub project https://github.com/dveselka/golang-hello

No comments:

Post a Comment