According to golang official github,
Modules are an experimental opt-in feature in Go 1.11, with the plan of incorporating feedback and finalizing the feature for Go 1.13.
The initial prototype (vgo
) was announced in February 2018. In July 2018, support for versioned modules landed in the main repository.
Example
Create a directory called test
and execute the below commands,
go mod init test
Inside the directory create files root.go
and main.go
Below is an example take from cobra project.
main.go
package main import ( "{pathToYourApp}/cmd" ) func main() { cmd.Execute() }
root.go
package cmd import ( "fmt" "github.com/spf13/cobra" "os" ) var rootCmd = &cobra.Command { Use: "hugo", Short: "Hugo is a very fast static site generator", Long: `A Fast and Flexible Static Site Generator built with love by spf13 and friends in Go. Complete documentation is available at http://hugo.spf13.com`, Run: func(cmd * cobra.Command, args[] string) { fmt.Println("this is cobra hello!") }, } func Execute() { if err: = rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } }
Execute
go build ./test this is cobra hello
The purpose of this article is to show how to create a go-module project and execute sample go files. Using go modules we can create our projects outside the $GOPATH.
Just a note: If you using an IDE, go with default go settings as show below, make sure you check the “index entire GOPATH”