33

If we have bin directory already where executables go then what is the need of pkg directory? Please explain.

2

3 Answers 3

30

The pkg directory contains Go package objects compiled from src directory Go source code packages, which are then used, at link time, to create the complete Go executable binary in the bin directory.

We can compile a package once, but link that object into many executables. For example, the fmt package appears in almost every Go program. It's compiled once but linked many times, a big saving.

3
  • So does this mean that packages other than main should go there
    – dvsakgec
    Commented Nov 18, 2017 at 19:50
  • 1
    @dvsakgec: Yes. An executable command has one and only one main package. It is linked with other imported packages from the pkg directory to form the exectable in the bin directory. Packages other than main are compiled to object form and stored in the pkg directory, which is searched at link time for imported packages.
    – peterSO
    Commented Nov 18, 2017 at 20:02
  • There are other uses for the pkg directory. See the answer by kyakya for more details.
    – Rich
    Commented Apr 1, 2022 at 15:31
13

~/go/pkg

From How to Write Go Code, we know ~/go/pkg can store third party library. For example:

  1. Create two file main.go and go.mod:
//main.go
package main

import (
    "fmt"
    "github.com/google/go-cmp/cmp"
)

func main() {
    fmt.Println(cmp.Diff("Hello World", "Hello Go"))
}
//go.mod
module example.com/user/hello

go 1.13
  1. Install a third party library
$ ls ~/go/pkg/mod/github.com/google/
...

$ go install example.com/user/hello
go: finding github.com/google/go-cmp v0.5.2
go: downloading github.com/google/go-cmp v0.5.2
go: extracting github.com/google/go-cmp v0.5.2

$ ls ~/go/pkg/mod/github.com/google/
... [email protected]

$ ls ~/go/pkg/mod/github.com/google/[email protected]

And then, you will see a lot of go files, not compile files.

$ ls ~/go/pkg/mod/github.com/google/[email protected]/cmp/
...  example_test.go   options.go   ...

pkg in user project

This pkg is a directory/package of user project. You can see it as Library and it's OK to use by external applications. For more things, you can check here.

1
  • 5
    This is the most complete answer. See also github.com/golang-standards/project-layout. There are a number of useful links in this repo that further explain, and provide examples of, the pkg directory, including the history of pkg and the pros/cons of using it.
    – Rich
    Commented Apr 1, 2022 at 15:24
7

You put your source code in src directory while pkg is the directory that holds compilation output of your actual source code. If you use multiple libraries/packages you will have different output with extension .a for each one, A linker should be responsible for linking and combining all of them together to produce one final executable in bin directory.

As pkg and bin are more specific to the machine or operating system into which you build your actual source code so it is not recommended to share both of them, your repo should have only your actual code.

A side note, if you plan to use docker containers, pkg dir should be ignored as we may build the source code in windows for example while you import/mount your code into linux container; at this time pkg will have compiled files that are only valid for windows

1
  • There are other uses for the pkg directory. See the answer by kyakya for additional details.
    – Rich
    Commented Apr 1, 2022 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.