DEV Community

Cover image for Module Management for Golang on Win10
Plume
Plume

Posted on • Edited on

Module Management for Golang on Win10

#go

Install and Config Golang on Win10

Create a Module

  1. Create a new directory, in this example, I Created "A:/Learn/Go/Work".

  2. Open cmd or PowerShell, jump to the new directory.
    cd /d A:/Learn/Go/Work

  3. Run go mod init Aoi/Sola where Aoi/Sola will be the new module's name. This will create a go.mod File in "A:/Learn/Go/Work", make "A:/Learn/Go/Work" the new module's root directory.

The go.mod file's content will be:

module Aoi/Sola

go 1.17 
Enter fullscreen mode Exit fullscreen mode

Where Aoi/Sola is the new module's name, 1.17 is Golang's version number.

Hello World

Create a source file in module's root directory(in this example "A:/Learn/Go/Work").

In this example, I created hello.go.

hello.go's content:

package main // "package main" specifies the package name: "main"

// Golang's main func have no return no argument.
func main(){
  println("Hello World") // Output "Hello World" through standard error.
}
Enter fullscreen mode Exit fullscreen mode

Every source file's first line should be a package <package_name> statement.
A Golang program can only have one "main" package.

Jump to module's root directory(in this example "A:/Learn/Go/Work"), run go run hello.go, you will see "Hello World" in standard error output.

Import Module

Import Remote Module

Create another module in another directory, in this example I created a module named "remote" in "A:/Learn/Go/Remote".
Create a source file in "A:/Learn/Go/Remote", I created remote.go.

remote.go's content:

package remote  // "hello.go" will refer variable Str with name "remote.Str", the package name "remote" is also used as namespace.

var Str = "I Love Aoi Sola" // Variable/Function name's first letter must be uppercase, or they can't be accessed out of current module.
Enter fullscreen mode Exit fullscreen mode

Then push "A:/Learn/Go/Remote" to a remote repository and change the module name to the repository's url:

Change Module Name1

Change Module Name2

Change hello.go's content to:

package main

import "github.com/ChengAnXu2014/GoModule"   // module name is also remote repository's URL.

func main(){
  println(remote.Str)    // refer varialbe Str with "remote.Str".
}
Enter fullscreen mode Exit fullscreen mode

Run go mod tidy in "A:/Learn/Go/Work" to upgrade module Aoi/Sola's go.mod file.
Golang will download module github.com/ChengAnXu2014/GoModule's content to the directory specified by environment variable GOPATH.

And module Aoi/Sola's go.mod file's content will be like:

module Aoi/Sola

go 1.17

require github.com/ChengAnXu2014/GoModule v0.0.0-20210624111742-626d6b55d79e // means current module require module `github.com/ChengAnXu2014/GoModule`.
// if any tags is created, the most recent tag's commit will be used, vx.x.x will be the most recent tag number.
// if no tag is created, the most recent commit is used, vx.x.x will be v0.0.0
// 20210624111742 is the used commit's commit time(2021/06/24 11:17:42).
// 626d6b55d79e is the used commit's name, and is also the used commit's checksum.

Enter fullscreen mode Exit fullscreen mode

For security, Golang won't access third-party repository by default, to change that you must set the environment variable GOPRIVATE(see below).

Now run go run hello.go in "A:/Learn/Go/Work", "I Love Aoi Sola" will be output through standard error.

If you changed remote module, then delete "require ..." statement from module Aoi/Sola's go.mod and run go mod tidy.
Golang will download from remote module and upgrade module Aoi/Sola's go.mod again.

GOPRIVATE

For security, Golang won't access third-party repository by default, to change that you must set the environment variable GOPRIVATE.

Set GOPRIVATE

Multiple URLs can be separated by ,.

Import Local Module

Change "A:/Learn/Go/Remote/remote.go" to:

package remote

var Str = "I'm Local Module!!!"
Enter fullscreen mode Exit fullscreen mode

Change module Aoi/Sola's go.mod to:

module Aoi/Sola

go 1.17

replace github.com/ChengAnXu2014/GoModule => A:/Learn/Go/Remote
Enter fullscreen mode Exit fullscreen mode

delete "require..." statement and add "replace..." statement to map github.com/ChengAnXu2014/GoModule to local directory "A:/Learn/Go/Remote"

Then run go mod tidy in "A:/Learn/Go/Work" to upgrade module Aoi/Sola's go.mod.

Run go run hello.go in "A:/Learn/Go/Work", you will see "I'm Local Module!!!" in standard error output.

Top comments (0)