Use goland to develop golang project environment configuration

Way 1: Use GOPATH and GOROOT


Open: Settings - Go in goland, and you will see GOROOT and GOPATH. Their related explanations and configurations are as follows:

  • GOROOT: Corresponds to the installation path of go, for example: D:\go\bin
  • GOPATH: is our workspace, which saves go code and third-party dependency packages

Multiple GOPATHs can be set. Among them, the first one will be the default package directory. The package downloaded using go get will be in the src directory in the first path. When using go install, in which GOPATH the package was found , the executable file will be generated in the bin directory under which GOPATH.

When using GOPATH, Go searches for packages in the following directories:

  • GOROOT/src: Go standard library
  • GOPATH/src: application's own code and third-party dependent code

When importing a package, the path starts from the file address of the lower level of src, and the compiler will automatically import it from under src, as follows:

// 自定义包的绝对路径为:D:\learing\goproject\src\gocode\testproject001\model
import (
    "fmt"
    "gocode/testproject001/model"
)

When using method 1 to import a custom package, the error is reported as follows:

main.go:5:2: package gocode/testproject001/model is not in std (D:\go\src\gocode\testproject001\model)

Note: This method is error-prone and is not recommended.

Method 2: Use go.mod


Starting from Go1.11, it is no longer recommended to use GOPATH. Instead, use GOMODULE to manage program files.

View the project's go configuration


First, enter the project directory in the terminal and execute go env to view the related configuration of go, as follows:

D:\learing\goproject\src\gocode\testproject001\main>go env

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\heliubei\AppData\Local\go-build
set GOENV=C:\Users\heliubei\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\learing\goproject\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\learing\goproject
set GOPRIVATE=
set GOPROXY=https://goproxy.io,direc
set GOROOT=D:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\heliubei\AppData\Local\Temp\go-build3874952850=/tmp/go-build -gno-record-gcc-switches

If GO111MODULE = off, you can modify it through the following command to ensure GO111MODULE = on

go env -w GO111MODULE = on

GO111MODULE Introduction


GO111MODULE has three values: off, on and auto. The details are as follows:

1) GO111MODULE=off, the go command line will not support the module function, and the method of finding dependent packages will continue to be used in the old version through the vendor directory or GOPATH mode.

2) GO111MODULE=on, the go command line will use modules and will not search in the GOPATH directory at all.

3) GO111MODULE=auto, the default value, the go command line will decide whether to enable the module function based on the current directory. This situation can be divided into two situations:

  • The current directory is outside GOPATH/src and contains the go.mod file
  • The current file is under the directory containing the go.mod file

go.mod uses


1) Enter the project path and execute the go mod init + module name command to create a go.mod file under the project file, as follows:

D:\learing\goproject>go mod init goproject

go: creating new go.mod: module goproject
go: to add module requirements and sums:
        go mod tidy

My project directory is: D:\learning\goproject

2) When introducing a custom package, the path should start from the path where go.mod is located.

package main

import (
    "fmt"
    "goproject/src/gocode/testproject001/model"
)

func main() {
    //创建person结构体示例:
    p := model.NewPerson("丽丽")
    p.SetAge(20)
    fmt.Println(p.Name)
    fmt.Println(p.GetAge())
    fmt.Println(*p)
}

The absolute path of the custom package is: D:\learning\goproject\src\gocode\testproject001\model

3) Successfully import the custom package

Guess you like

Origin blog.csdn.net/weixin_47156401/article/details/134564505