Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct directory structure for a Go Project?

Tags:

go

I'm relatively new to Go and I've recently created a project that's going up on GitHub. I've tried to follow guides but there's a pressing question of why my binaries end up in src/?

My layout is like this:

ssm/ - Name of project
    LICENSE
    README.md
    src/ - Source Code
        files.go - All my source code is here.
        src - The compiled binary ends up here
    bin/ - Binaries

I set my $GOPATH to ~/Documents/Programming/Go/. From my gopath, I can't type go build ssm because it cannot find package. If I cd into the directory, it complains it can't load package: package .: no Go source files.

I have to actually go into src and compile there, which means the binary isn't in bin/.

What am I doing wrong?

like image 513
Mark Avatar asked Sep 30 '13 17:09

Mark


People also ask

Where are go projects stored?

In Go, programs are kept in a directory hierarchy that is called a workspace. A workspace is simply a root directory of your Go applications. A workspace contains three subdirectories at its root: src – This directory contains source files organized as packages.


Video Answer


2 Answers

See https://code.google.com/p/go-wiki/wiki/GithubCodeLayout

To be compatible with go get, your project's package name needs to be fully-qualified under the github.com domain:

$GOPATH/
    src/github.com/<user>/ssm/
        .git
        LICENSE
        README.md
        files.go
    bin/

Note that the base of the git repository (.git) is not the same as the $GOPATH.

Also, go build <package> will output a compiled executable to the current directory. If you want the exe to go to bin/, use go install <package> instead.

like image 163
lnmx Avatar answered Oct 21 '22 10:10

lnmx


Your go code you can kept in a workspace. A workspace contains many source files (git, hg, svm, etc.). The Go tool understand the layout. We don't require Makefile or build.xml here. The basic directory layout is everything. If in any case if you are going to change the file layout, accordingly you need to change the build.

This is the general structure you can follow,

$GOPATH/

src/
    github.com/username/repo/
        mypkg/
            mysrc1.go
            mysrc2.go
        cmd/mycmd/
            main.go
bin/
    mycmd

And, this is the standard workspace

$GOPATH/

bin/fixhub                              # installed binary
pkg/darwin_amd64/                       # compiled archives
    code.google.com/p/goauth2/oauth.a
    github.com/...
src/                                    # source repositories
    code.google.com/p/goauth2/
        .hg
        oauth                           # used by package go-github
        ...
    github.com/
        golang/lint/...                 # used by package fixhub
            .git
        google/go-github/...            # used by package fixhub
            .git
        dsymonds/fixhub/
            .git
            client.go
            cmd/fixhub/fixhub.go        # package main

go get fetch many repositories whereas go install builds a binary out of them. It's convenient and easy to go for go development quickly. And, everyone in the go community follows the same. This puts src, bin and pkg into the home directory. And, $HOME/bin is already in our path before creating our workspace.

like image 20
mohan08p Avatar answered Oct 21 '22 09:10

mohan08p