I've just written a short step-by-step guide on how I am using the new go tool and github.com. You might find it useful:
1. Setup your GOPATH
You can set the environment variable GOPATH
to any directory you like. If you have larger projects, it's probably a good idea to create a different GOPATH for each of them. I would recommend this approach especially for the deployment, so that updating a library for project A doesn't break project B which might require an earlier version of the very same library.
Also note that you can set your GOPATH to a list of directories, delimited by colons. So you might have a GOPATH containing all commonly used packages, and separate GOPATHS for each project with additonal packages or different versions of existing packages.
But unless your are working on a lot of different Go projects simultaneously, its probably enough to have just a single GOPATH locally. So, let's create one:
mkdir $HOME/gopath
Then you need to set two environment variables to tell the go tool where it can find existing Go packages and where it should install new ones. It's probably best to add the following two lines to your ~/.bashrc
or ~/.profile
(and do not forget to reload your .bashrc afterwards).
export GOPATH="$HOME/gopath"
export PATH="$GOPATH/bin:$PATH"
2. Create a new project
If you want to create a new Go project which should be hosted at github.com later, you should create this project under $GOPATH/src/github.com/myname/myproject
. It's important that the path matches the URL of the github.com repo, because the go tool will follow the same convention. So, let's create the project root and initialize a new git repository there:
mkdir -p $GOPATH/src/github.com/myname/myproject
cd $GOPATH/src/github.com/myname/myproject
git init
Because I do not like to type such long paths, I normally create symbolic links for the projects I am currently working on in my home folder:
ln -s $GOPATH/src/github.com/myname/myproject ~/myproject
3. Write your application
Start coding and don't forget to git add
and git commit
your files. Also, do not use relative imports like import "./utils"
for sub-packages. They are currently undocumented and shouldn't be used at all, because they won't work with the go tool. Use imports like github.com/myname/myproject/utils
instead.
4. Publish your project
Create a new repository at github.com, upload your SSH public key if you haven't done that before and push your changes to the remote repository:
git remote add origin git@github.com:myname/myproject.git
git push origin master
5. Continue working on your project
If you have set the GOPATH in your .bashrc and if you have created a symlink to your project in your home folder, you can just type cd myproject/
and edit some files there. Afterwards, you can commit the changes using git commit -a
and send them to github.com by doing a git push
.
With modules introduction in Go v1.11 and above you don't have to specify your project path in $GOPATH/src
You need to tell Go about where each module located by creating go.mod file. Please refer to go help mod
documentation.
Here is an example of how to do it:
project
| go.mod
| main.go
|
\---internal
+---bar
| bar.go
| go.mod
|
\---foo
foo.go
go.mod
project/internal/bar/go.mod
module bar
go 1.14
project/internal/bar/bar.go
package bar
import "fmt"
//Bar prints "Hello from Bar"
func Bar() {
fmt.Println("Hello from Bar")
}
project/internal/foo/go.mod
module foo
go 1.14
project/internal/foo/foo.go
package foo
import "fmt"
//Foo prints "Hello from Foo"
func Foo() {
fmt.Println("Hello from Foo")
}
project/main.go
package main
import (
"internal/bar"
"internal/foo"
)
func main() {
bar.Bar()
foo.Foo()
}
Now the most important module
project/go.mod
module project
go 1.14
require internal/bar v1.0.0
replace internal/bar => ./internal/bar
require internal/foo v1.0.0
replace internal/foo => ./internal/foo
Couple things here:
- You can have any name in require. You can have project/internal/bar if you wish. What Go think it is URL address for the package, so it will try to pull it from web and give you error
go: internal/bar@v1.0.0: malformed module path "internal/bar": missing dot in first path element
That is the reason why you need to have replace
where you tell Go where to find it, and that is the key!
replace internal/bar => ./internal/bar
- The version doesn't matter in this case. You can have v0.0.0 and it will work.
Now, when you execute your code you will have
Hello from Bar
Hello from Foo
Here is GitHub link for this code example
Best Answer
Let me define this first
modules
are collections ofpackages
. In Go 11, I use go modules like the following:If both packages are in the same project, you could just do the following: In
go.mod
:module github.com/userName/moduleName
and inside your
main.go
import "github.com/userName/moduleName/platform"
However, if they are separate modules, i.e different physical paths and you still want to import local
packages
without publishing this remotely to github for example, you could achieve this by usingreplace
directive.Given the module name
github.com/otherModule
andplatform
, as you've called it, is the only package inside there. In your main module'sgo.mod
add the following lines:Note: The path should point to the root directory of the module, and can be absolute or relative.
Inside
main.go
, to import a specific package likeplatform
fromotherModule
:Here's a gentle introduction to Golang Modules