Skip to content

Example to show package structure of simple go code

Notifications You must be signed in to change notification settings

U24Lab/goModuleExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example:

Why?

How to use packages in Go using 1.16 Go version:

When we have GO111MODULE set to on Go will automatically try to find packages within the project structure.

What?

  1. We have a main module which uses two modules goodbye and hello

  2. Make sure the Go Modules is On:

export GO111MODULE="on"

  1. Start a go module with go mod init example/main, you will also see many examples using go mod init example.com/main/. The example.com note the .com sometimes is used as a repository so in the future, we can use go get <module_name like example.com/subpackage> to download from the internet(or repository) and if the new sub-package need to be used in the code.

  2. go mod init <Module Name> will create the Module in the main which is the place where your code execution should start. It needs to be done once in the root path of the Package/Module.

  3. Two ways we can Structure the project and packages(As per new standard of 1.16 Go version and as far my knowledge also ;) ):

5.1 Using Just Folder Names

You can see the branch withoutmod to see how a structuring can be maintain using folder names as Package paths.

5.2 Using Generic Names and making each subpackage having mod files. In this case, you need additional go.mod in each package and here the path can be URL as well, which you will see most commonly.

In this Git project master branch code shows this type of structure.

  1. go mod tidy is a useful command to clean up the structuring of the module in the go project if any changes are made.

  2. Additional Read: Go Tutorial from Official Site

Common Issue: Package / is not in GOROOT

Reason: Go is not able to understand your structure, do check the spellings on packages and folder properly.

About

Example to show package structure of simple go code

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages