Install and Config Golang on Win10
Create a Module
Create a new directory, in this example, I Created "A:/Learn/Go/Work".
Open cmd or PowerShell, jump to the new directory.
cd /d A:/Learn/Go/Work
Run
go mod init Aoi/Sola
whereAoi/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
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.
}
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.
Then push "A:/Learn/Go/Remote" to a remote repository and change the module name to the repository's url:
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".
}
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.
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.
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!!!"
Change module Aoi/Sola
's go.mod to:
module Aoi/Sola
go 1.17
replace github.com/ChengAnXu2014/GoModule => A:/Learn/Go/Remote
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)