Skip to content

001. Go dev setup

Tanveer Alam edited this page Sep 17, 2019 · 2 revisions

GO Dev setup

GOPATH:

  • lists places to look for go code also used to resolve go imports.
  • default location: $HOME/go
  • $ go env GOPATH
    /home/tan/go
  • $ ls -1 $(go env GOPATH)
    bin
    pkg
    src
  • src:
    • Holds the source code.
    • Path below src determines the import path or executable name.
  • pkg:
    • Holds installed package objects.
    • Each targeting os and arch has its own subdirectory of pkg (pkg/GOOS_GOARCH)
    • $ go env GOOS GOARCH
      linux
      amd64
    • Suppose our GOPATH is /home/tan/go, a package with source $GOPATH/src/foo/bar can be imported as "foo/bar" and has its compiled form installed to "GOPATH/pkg/GOOS_GOARCH/foo/bar.a"

-bin:

  • Holds compiled commands.

  • Each command is named by its source directory(final element, not the entire path).

    • So $GOPATH/src/foo/quux is installed into $GOPATH/bin/quux not $GOPATH/bin/foo/quux
  • GOBIN - $ go env GOBIN If the GOBIN environment variable is set, commands are installed to the directory it names instead of GOPATH/bin.

    • GOBIN must be an absolute path.
GOPATH=/home/tan/go

/home/tan/go/
    src/
        foo/
            bar/               (go code in package bar)
                x.go
            quux/              (go code in package main)
                y.go
    bin/
        quux                   (installed command)
    pkg/
        linux_amd64/
            foo/
                bar.a          (installed package object)

___________________________________________________________--- References:

Clone this wiki locally