Go under the Windows environment configuration & installation & compile and run

Go download and install

Go official download site: https://golang.google.cn/
can also select: https://studygolang.com/dl

Configuration environment variable

Common Environment Variables

  • GOROOT

GOROOTGo is the installation directory, in Windows, the default location is GOROOT C:/go, and the default location on Mac OS or Linux in GOROOT is /usr/local/go, if Go installed in another directory, but the location needs to be modified to GOROOT corresponding directory.

In addition, GOROOT/binunder the chain tool included Go to our offer, so it should be GOROOT/binconfigured to the PATH environment variable to help us use the Go tool in the global chain.

For example: Now Go installation directory D: \ WindowsSoftware \ Golang, you need to add in a system variableGOROOT, the valueD:\WindowsSoftware\Golang
GOROOT

  • Gofatः

GOPATHGo language is the working directory.
go install / go get go and the tools will be used GOPATH environment variable.
GOPATH as compiled binary search path when the storage destination and import package.
GOPATH mainly includes three directories: bin, pkg,src

  • bin: The main executable file is stored.
  • pkg: store the compiled library files, mainly * .a file.
  • src: under the main source files stored go.
    It also should be noted that not to speak path City Go GOROOT set the language to avoid unnecessary conflicts.

GOPATH can set up multiple workspaces, but when we go get use to get remote commands library, usually the first to install a workspace them.

Each work area with a semicolon, it can split.

export GOPATH=/opt/go;$home/go

For example: a work area in D: \ GoPath, need to be added in the system variableGOPATH, the value ofD:\GoPath
Gofatः

  • GOBIN
    GOBIN our binary installation directory command after the development program is compiled.

When we use the command to compile and go install packaged applications when, after the command will compiled binaries packaged GOBIN directory, usually we will GOBIN set GOPATH / bin.

For example: found in the system variablepath, add value%GOROOT%\binand%GOPATH%\bin
path

  • Detecting whether the configuration is successful
    open CMD, entergo env
    go env

    Not commonly used environment variables

  • GOOS and GOARCH

GOOS and GOARCH when the need for cross-platform environment variable compile time, you need to set up this way is called cross-compiler compiler.

The so-called cross-compiler, refers on one platform will generate code that can run on another platform, for example, we can develop on 32-bit Windows operating system, and then generates a binary that can run on 64-bit Linux operating system process.

GOOS: The default value is our current operating systems such as Windows, Linux, it should be noted that the corresponding value of the Mac OS is darwin.
GOARCH: represents the CPU architecture, such as 386, amd64, arm and so on.
You can get the current value of GOOS through the GOARCH go env.

$ go env GOOS GOARCH
darwin
amd64

GOOS and GOARCH range of values.
GOOS GOARCH value pairs occurs, and only the following is a list of the corresponding value.

GOOS        GOARCH
------------------
android     arm
darwin      386
darwin      amd64
darwin      arm
darwin      arm64
dragonfly   amd64
freebsd     386
freebsd     amd64
freebsd     arm
linux       386
linux       amd64
linux       arm
linux       arm64
linux       ppc64
linux       ppc64le
linux       mips
linux       mipsle
linux       mips64
linux       mips64le
linux       s390x
netbsd      386
netbsd      amd64
netbsd      arm
openbsd     386
openbsd     amd64
openbsd     arm
plan9       386
plan9       amd64
solaris     amd64
windows     386
windows     amd64

Compile target programs that run on 64-bit Linux operating system

$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go

Object program compiled on the Android operating arm architecture

$ CGO_ENABLED=0 GOOS=android GOARCH=arm GOARM=7 go build main.go
  • All list of environment variables

Although we generally Although the configuration of environment variables had a few, but in fact the Go language is to provide a lot of environment variables, so we are free to custom development and compiler behavior.

下面是Go提供的所有的环境变量列表,一般可以划分为下面几大类,大概了解一下就可以了,因为有些环境变量我们可以永远都不会用到。

GCCGO
GOARCH
GOBIN
GOCACHE
GOFLAGS
GOOS
GOPATH
GOPROXY
GORACE
GOROOT
GOTMPDIR
  • 和cgo一起使用的环境变量
CC
CGO_ENABLED   // 禁用cgo
CGO_CFLAGS
CGO_CFLAGS_ALLOW
CGO_CFLAGS_DISALLOW
CGO_CPPFLAGS, CGO_CPPFLAGS_ALLOW, CGO_CPPFLAGS_DISALLOW
CGO_CXXFLAGS, CGO_CXXFLAGS_ALLOW, CGO_CXXFLAGS_DISALLOW
CGO_FFLAGS, CGO_FFLAGS_ALLOW, CGO_FFLAGS_DISALLOW
CGO_LDFLAGS, CGO_LDFLAGS_ALLOW, CGO_LDFLAGS_DISALLOW
CXX
PKG_CONFIG
AR
  • 与系统架构体系相关的环境变量
GOARM
GO386
GOMIPS
GOMIPS64
  • 专用的环境变量
GCCGOTOOLDIR
GOROOT_FINAL
GO_EXTLINK_ENABLED
GIT_ALLOW_PROTOCOL
  • 所有环境变量列表
GOEXE
GOHOSTARCH
GOHOSTOS
GOMOD
GOTOOLDIR

目录结构

  • 目前流行的项目结构
├── bin  # 存放编译后的二进制文件
├── pkg  # 存放编译后的库文件
└── src  # 存放源码文件
    └── code.fdevops.com  # 使用网站域名区分项目
        └── lanyulei      # 作者/部门/机构...
            └── demo1     # 项目
                └── main.go

推荐编辑器

Go采用的是UTF-8编码的文本文件存放代码的,理论上使用任何一款文件编辑器都可以做Go语言开发,这里主要推荐两个开发工具。

  • VS Code

Visual Studio Code(简称VS Code)是一个由微软开发的,同时支持Windows、Linux、和macOS系统且开放源代码的代码编辑器,它支持测试,并内置了Git 版本控制功能,同时也具有开发环境功能,例如代码补全(类似于 IntelliSense)、代码片段、和代码重构等,该编辑器支持用户个性化配置,例如改变主题颜色、键盘快捷方式等各种属性和参数,还在编辑器中内置了扩展程序管理的功能。

虽然不如一些IDE功能强大,但是它添加Go扩展插件后已经足够胜任我们日常的Go开发工作了,而且它占用资源较少,所以就算配置较低的电脑也可以使用。

  • Goland

喜欢用IDE做开发的同学必定不能错过Jetbrains家族的IDE,款款精品,可谓都是IDE中的神兵利器。

下面介绍的就是Jetbrains家族中开发Go语言的Goland。

Goland是付费的,当然网上也会有一些激活码可以直接激活使用,但是个人认为,如果条件允许的话,希望购买正版,若是不愿意付费的话,建议使用VS Code也可满足正常开发需求。

编写第一个Go程序

  • hello.go
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello Go!")
}
  • 编译
go build hello.go

go build命令可以将Go语言程序代码编译成二进制的可执行文件,但是需要我们手动运行该二进制文件.

go run hello.go

go run命令则更加方便,它会在编译后直接运行Go语言程序,编译过程中会产生一个临时文件,但不会生成可执行文件,这个特点很适合用来调试程序。

  • 运行
./hello

参考:

https://blog.51cto.com/11293981/2417803
http://c.biancheng.net/view/6046.html

Guess you like

Origin www.cnblogs.com/WindSun/p/12142624.html