Skip to content

External modules

Initializing a module

go mod init github.com/hkirat/go-http-server

Naming a module

When you run go mod init to create a module for tracking dependencies, you specify a module path that serves as the module’s name. The module path becomes the import path prefix for packages in the module. Be sure to specify a module path that won’t conflict with the module path of other modules.

At a minimum, a module path need only indicate something about its origin, such as a company or author or owner name. But the path might also be more descriptive about what the module is or does.

Pushing a module

  • Initialize the package
go mod init github.com/hkirat/go-utils
  • Initialise a new package (worker.go)
package worker
func Sum(a int, b int) int {
return a + b
}
  • Push the repo to github
git add .
git commit -m "Init"
git remote add origin https://github.com/hkirat/go-utils
git push origin HEAD

Getting external dependencies

  • Create a new Go project
  • Get the external package we created in the last section
go get github.com/hkirat/go-utils
  • Try using the Sum function
package main
import worker "github.com/hkirat/go-utils"
func main() {
x := worker.Sum(1, 2)
println(x)
}