I've used GOPATH
but for this current issue I'm facing it does not help. I want to be able to create packages that are specific to a project:
myproject/
+-- binary1.go
+-- binary2.go
+-- package1.go
+-- package2.go
I tried multiple ways but how do I get package1.go
to work in the binary1.go
or the binary2.go
and so on?
For example; I want to be able to import "package1"
and then be able to run go build binary1.go
and everything works fine without the error being thrown that the package cannot be found on GOROOT
or GOPATH
. The reason why I need this kind of functionality is for large scale projects; I do not want to have to reference multiple other packages or keep them in one large file.
Since the introduction of go.mod , I think both local and external package management becomes easier. Using go.mod, it is possible to have go project outside the GOPATH as well.
Create a folder demoproject and run following command to generate go.mod file
go mod init demoproject
I have a project structure like below inside the demoproject directory.
+-- go.mod
+-- src
+-- main.go
+-- model
+-- model.go
For the demo purpose, insert the following code in the model.go file.
package model
type Employee struct {
Id int32
FirstName string
LastName string
BadgeNumber int32
}
In main.go, I imported Employee model by referencing to "demoproject/src/model"
package main
import (
"demoproject/src/model"
"fmt"
)
func main() {
fmt.Printf("Main Function")
var employee = model.Employee{
Id: 1,
FirstName: "First name",
LastName: "Last Name",
BadgeNumber: 1000,
}
fmt.Printf(employee.FirstName)
}
Just run go get
command inside the project directory.
For example:
go get -u google.golang.org/grpc
It should include module dependency in the go.mod file
module demoproject
go 1.13
require (
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect
golang.org/x/text v0.3.2 // indirect
google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150 // indirect
google.golang.org/grpc v1.26.0 // indirect
)
There's no such thing as "local package". The organization of packages on a disk is orthogonal to any parent/child relations of packages. The only real hierarchy formed by packages is the dependency tree, which in the general case does not reflect the directory tree.
Just use
import "myproject/packageN"
and don't fight the build system for no good reason. Saving a dozen of characters per import in any non trivial program is not a good reason, because, for example, projects with relative import paths are not go-gettable.
The concept of import paths have some important properties:
All of the above is ruined by using relative import paths. Do not do it.
PS: There are few places in the legacy code in Go compiler tests which use relative imports. ATM, this is the only reason why relative imports are supported at all.
I have a similar problem and the solution I am currently using uses Go 1.11 modules. I have the following structure
- projects
- go.mod
- go.sum
- project1
- main.go
- project2
- main.go
- package1
- lib.go
- package2
- lib.go
And I am able to import package1 and package2 from project1 and project2 by using
import (
"projects/package1"
"projects/package2"
)
After running go mod init projects
. I can use go build
from project1 and project2 directories or I can do go build -o project1/exe project1/*.go
from the projects directory.
The downside of this method is that all your projects end up sharing the same dependency list in go.mod. I am still looking for a solution to this problem, but it looks like it might be fundamental.
To add a "local" package to your project, add a folder (for example "package_name"). And put your implementation files in that folder.
src/github.com/GithubUser/myproject/
+-- main.go
+---package_name
+-- whatever_name1.go
+-- whatever_name2.go
In your package main
do this:
import "github.com/GithubUser/myproject/package_name"
Where package_name
is the folder name and it must match the package name used in files whatever_name1.go and whatever_name2.go. In other words all files with a sub-directory should be of the same package.
You can further nest more subdirectories as long as you specify the whole path to the parent folder in the import.
Perhaps you're trying to modularize your package. I'm assuming that package1
and package2
are, in a way, part of the same package but for readability you're splitting those into multiple files.
If the previous case was yours, you could use the same package name into those multiples files and it will be like if there were the same file.
This is an example:
add.go
package math
func add(n1, n2 int) int {
return n1 + n2
}
subtract.go
package math
func subtract(n1, n2 int) int {
return n1 - n2
}
donothing.go
package math
func donothing(n1, n2 int) int {
s := add(n1, n2)
s = subtract(n1, n2)
return s
}
I am not a Go expert and this is my first post in StackOveflow, so if you have some advice it will be well received.
You can use replace
go mod init example.com/my/foo
foo/go.mod
module example.com/my/foo
go 1.14
replace example.com/my/bar => /path/to/bar
require example.com/my/bar v1.0.0
foo/main.go
package main
import "example.com/bar"
func main() {
bar.MyFunc()
}
bar/go.mod
module github.com/my/bar
go 1.14
bar/fn.go
package github.com/my/bar
import "fmt"
func MyFunc() {
fmt.Printf("hello")
}
Importing a local package is just like importing an external pacakge
except inside the go.mod file you replace that external package name with a local folder.
The path to the folder can be full or relative /path/to/bar
or ../bar
Run:
go mod init yellow
Then create a file yellow.go
:
package yellow
func Mix(s string) string {
return s + "Yellow"
}
Then create a file orange/orange.go
:
package main
import "yellow"
func main() {
s := yellow.Mix("Red")
println(s)
}
Then build:
go build
Source: Stackoverflow.com