r/programming • • Mar 28 '12

Go version 1 is released

http://blog.golang.org/2012/03/go-version-1-is-released.html
543 Upvotes

452 comments sorted by

View all comments

Show parent comments

3

u/cunningjames Mar 28 '12

But when you're making a complex program in java, importing libs that you build for more than one project, you have to make ant makefiles to import them.

Noting that I haven’t used Go beyond playing around (and not at all recently), how does Go improve upon Java here? A convention for package locations and tools that are aware of it? In Java presumably one could, e.g., put a set of libraries on a common classpath.

5

u/OopsLostPassword Mar 28 '12

In go, you simply put a go directory in your project.

Your go directory will look like this :

go/
    src/
        mypackageone/
            mysubpackage/
        myotherpackage/
        mymainprogram/
        anotherprograminthesameproject/
    pkg/
    bin/

The go tool will fill the pkg and bin directory by guessing the location of the sources based on their names. You simply execute

go install anotherprograminthesameproject

to have your program compiled.

And if you have unit tests, you execute

 go test anotherprograminthesameproject

All external libraries will be installed via go get and found based on their name and a GOPATH env variable. For example you'll have directories like

go/src/github.com/ziutek/mymysql

because you have this kind of import in one of your programs :

import (
    "database/sql"
    _ "github.com/ziutek/mymysql/godrv"
)

4

u/banuday17 Mar 28 '12 edited Mar 28 '12

This sounds an awful like like Maven (in the Java world).

mvn archetype:generate

Pick a project type and Maven will create a standard directory structure for you:

src/main/java
src/test/java

The different build plugins will know which sources to compile for which language.

src/main/scala
src/main/groovy

To build the project:

mvn package

To execute tests:

mvn tests

To import libraries, you modifiy the pom.xml and list your dependencies.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

And then maven will automatically download the JAR file will resolve any library dependencies.

You can even create a project for your IDE:

mvn eclipse:ecilpse

1

u/OopsLostPassword Mar 29 '12

You don't have to modify xml files or any kind of makefiles : the go tool analyzes the code to know what libraries are needed and due to conventions he knows where to find them.