Creating a library

1,152 views
Skip to first unread message

Jeremy Villalobos

unread,
Apr 27, 2012, 4:01:57 PM4/27/12
to golan...@googlegroups.com
This may seem fairly obvious, but it is not in the documentation pages.  

How do I manage packages on my own project ?  If I want to make a module for some related services for example ?  The equivalent of creating a package in Java ( com.my.company. )

How do I create a library for internal use ?

How do I compile multiple files ?

What is the standard makefile or ant script that is intended to be used by go projects ?

Thank you.

Kyle Lemons

unread,
Apr 27, 2012, 4:05:15 PM4/27/12
to Jeremy Villalobos, golan...@googlegroups.com
They're all packages in a GOPATH.  Whether you publish them on the web makes them internal or not :).

Have you read How to Write Go Code http://golang.org/doc/code.html ?

Jason McVetta

unread,
Apr 27, 2012, 6:53:37 PM4/27/12
to Jeremy Villalobos, golan...@googlegroups.com
Your question seems similar to my recent question about code layout.  Let me try to answer with an example.  Suppose you want to create a application 'politics' that depends on two of your libraries, 'corruption' and 'greed'.  The app and both libraries live on github, each in its own repository.  Your code layout would look like this:

$GOPATH/
    src/
        github.com/
            your_username/
                corruption/
                    .git/
                    corruption.go
                greed/
                    .git/
                    greed.go
                politics/
                    .git/
                    politics.go

Each folder under src/github.com/your_username/ is the root of a git checkout.  Let's assume we are starting from scratch, and the repos for all three packages have already been initialized on github.  Use the "Initialize this repository with a README" option so your repos can be cloned immediately.   You would create the project like this:

cd ~
mkdir mygo  # Go workspace
export GOPATH=~/mygo
cd $GOPATH
git clone g...@github.com:your_username/corruption.git
git clone g...@github.com:your_username/greed.git
git clone g...@github.com:your_username/politics.git


Create your greed & corruption libraries.  You would then import them into politics.go like this:

import(
)


During development you can build e.g. the greed library by itself with the command "go build ...greed".  To compile the politics app and its dependencies all together, you just need to give the command "go build ...politics".  




On Fri, Apr 27, 2012 at 1:01 PM, Jeremy Villalobos <jeremyvi...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages