Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install GO with source code

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to source code installation GO", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to source code installation GO" this article.

Analyze the installation script

Several scripts are mentioned in the article "exploring the functions of various directories in Go" and their role. Now let's analyze what these scripts have done. (take linux as an example, the script corresponding to Windows is similar)

1. All.bash1set-e2if [!-f make.bash]; then3echo 'all.bash must be run from $GOROOT/src' 1 > & 24exit 15fi6OLDPATH = "$PATH" 7. / make.bash-- no-banner8bash run.bash-- no-rebuild9PATH= "$OLDPATH" 10$ GOTOOLDIR/dist banner # print build info

Description:

1) set-e the script exits when a command in the script returns a non-zero exit status

2) the if statement requires that all.bash must be run in the same directory as make.bash (that is, $GOROOT/src)

3) OLDPATH= "$PATH" saves the current PATH

4) execute make.bash and pass the-no-banner parameter

5) execute run.bash and pass the-no-rebuild parameter

6) execute dist tool and pass banner parameters, print build information

2 、 make.bash

In make.bash, there are some environment variables that are used during make.bash execution.

① GOROOT_FINAL: the Go root that is finally set, which defaults to the location of Go tree during compilation.

② GOHOSTARCH: specifies the architecture for host tools (compilers and binaries). Binaries of this type must be executable on the current system, so the only common reason for setting this environment variable is to set GOHOSTARCH=386 on the AMD64 machine. (that is, you can run 386 executable files on ADM64)

③ GO_GCFLAGS: when building packages and commands, you can attach the parameters of 5g/6g/8g through this environment variable

④ GO_LDFLAGS: when building packages and commands, you can attach the parameters of 5l/6l/8l through this environment variable

⑤ CGO_ENABLED: controls the use of cgo during the build process. When set to 1, all cgo-related files, such as .c and .go files with "cgo" compilation instructions, are included when you build. When set to 0, ignore them (that is, disable CGO)

At the beginning of the file are some checks: such as whether the version of make.bash,ld is running under Windows, and so on.

The next main work in make.bash (that is, start building):

1) build a C boot tool-- cmd/dist

First of all, we will export GOROOT the environment variable, whose value is the path where the go source code is located. As you can see, GOROOT is not required before the source code is installed.

Learn a knowledge of shell script here

GOROOT_FINAL= "${GOROOT_FINAL:-$GOROOT}"

This is called parameter substitution and is in the following form:

${parameter-default}, ${parameter:-default}

If parameter is not set, then use default. The two forms are the same most of the time. The difference is that when parameter is set and empty, the first cannot output default values, while the second can.

So, GOROOT_FINAL= "${GOROOT_FINAL:-$GOROOT}" means that if GOROOT_FINAL is not set, then GOROOT_FINAL=$GOROOT

Compiling cmd/dist with gcc

2) build compiler and Go bootstrap tool

First set the required environment variables by executing dist

Eval $(. / cmd/dist/dist env-p)

Then build Go bootstrap tools: go_bootstrap, compiler, etc.

3) build packages and command tools

This is done through go_bootstrap.

3 、 run.bash

This script is a test script that runs the test cases in the standard library. Go packages and tools are rebuilt by default. Because make.bash does the work of building, when run.bash is executed in all.bash,-no-rebuild is passed

2. Installation instructions for source code

Source code installation of the GE language, generally only need to execute. / all.bash on it. (execute all.bat on Windows)

Based on the above analysis, this runs the test script. If you want to install Go faster, you can run make.bash directly (make.bat on Windows)

The entire installation process is roughly as follows:

# Building C bootstrap tool.

Cmd/dist

# Building compilers and Go bootstrap tool for host, linux/amd64.

Lib9

Libbio

Libmach

Misc/pprof

……

Pkg/text/template/parse

Pkg/text/template

Pkg/go/doc

Pkg/go/build

Cmd/go

# Building packages and commands for linux/amd64.

Runtime

Errors

Sync/atomic

Sync

Io

……

Testing

Testing/iotest

Testing/quick

# Testing packages.

Ok cmd/api 0.031s

? Cmd/cgo [no test files]

Ok cmd/fix 3.558s

Ok cmd/go 0.022s

……

? Unsafe [no test files]

Real 3m6.056s

User 2m29.517s

Sys 0m25.134s

# GOMAXPROCS=2 runtime-cpu=1,2,4

Ok runtime 6.605s

# sync-cpu=10

Ok sync 0.428s

# Testing race detector.

Ok flag 1.044s

#.. / misc/cgo/stdio

#.. / misc/cgo/life

#.. / misc/cgo/test

Scatter = 0 × 430490

Hello from C

PASS

Ok _ / home/polaris/go/misc/cgo/test 1.137s

#.. / misc/cgo/testso

#.. / doc/progs

Real 0m19.110s

User 0m15.341s

Sys 0m2.904s

#.. / doc/articles/wiki

Run.bash: line 92: make: command not found

PASS

#.. / doc/codewalk

#.. / misc/dashboard/builder.. / misc/goplay

#.. / test/bench/shootout

Fasta

Reverse-complement

Nbody

Binary-tree

Binary-tree-freelist

Fannkuch

Fannkuch-parallel

Regex-dna

Regex-dna-parallel

Spectral-norm

K-nucleotide

K-nucleotide-parallel

Mandelbrot

Meteor-contest

Pidigits

Threadring

Chameneosredux

#.. / test/bench/go1

Ok _ / home/polaris/go/test/bench/go1 4.942s

#.. / test

Real 1m38.036s

User 1m14.701s

Sys 0m16.645s

# Checking API compatibility.

+ pkg crypto/x509, const PEMCipher3DES PEMCipher

+ pkg crypto/x509, const PEMCipherAES128 PEMCipher

+ pkg crypto/x509, const PEMCipherAES192 PEMCipher

+ pkg crypto/x509, const PEMCipherAES256 PEMCipher

+ pkg crypto/x509, const PEMCipherDES PEMCipher

+ pkg crypto/x509, func EncryptPEMBlock (io.Reader, string, [] byte, [] byte, PEMCipher)

……

+ pkg reflect, func SliceOf (Type) Type

+ pkg regexp, method (* Regexp) Split (string, int) [] string

~ pkg text/template/parse, type DotNode bool

~ pkg text/template/parse, type Node interface {Copy, String, Type}

ALL TESTS PASSED

-

Installed Go for linux/amd64 in / home/polaris/go

Installed commands in / home/polaris/go/bin

* You need to add / home/polaris/go/bin to your PATH.

III. Scripts with Chinese comments

Here is the make.bash script I annotated (the key part)

The current Go source code of 1echo'# Building C bootstrap tool.'2echo cmd/dist3# export is in the directory GOROOT4export GOROOT= "$(cd. & & pwd)". If GOROOT_FINAL is not set, the value of $GOROOT is used as GOROOT_FINAL6GOROOT_FINAL= "${GOROOT_FINAL:-$GOROOT}" 7DEFGOOTROOTROOTRACTFINALL = "" $GOROOT_FINAL "'" 8.

If you compile Go itself to 32 bits on an amd64 machine, you can set $GOHOSTARCH=386. It is not recommended to do this 10mcm = "" 11case "$GOHOSTARCH" in12386) mflag=-m32;;13amd64) mflag=-m64;;14esac15

"gcc compile: compile all c files under cmd/dist"-m: specify the processor architecture for optimization (- M32,-M64) or empty (usually empty)-O: optimization option, generally:-O2. The optimized program is smaller than the unoptimized program. The execution speed may also be improved.-Wall: generate all warning messages 2 warning-Werror: all warnings become errors 2 debugging-ggdb: generate debug information for gdb (- g generates debug information) 2 debugging-o: generate specified output file 2 output-I: specify additional file search path 2 extra-D: equivalent to # define GOROOT_FINAL= "$GOROOT in C language _ FINAL "25gcc $mflag-O2-Wall-Werror-ggdb-o cmd/dist/dist-Icmd/dist" $DEFGOROOT "cmd/dist/*.c26

After compiling the dist tool, run dist. The purpose is to set the relevant environment variable 28 # for example: $GOTOOLDIR environment variable is the 29eval $(. / cmd/dist/dist env-p) 30echo31 set here

3The dist-tool parameter can be passed when running make.bash to compile only the dist tool 33if ["$1" = "--dist-tool"]; then34# Stop after building dist tool.35mkdir-p "$GOTOOLDIR" 36if ["$2"! = "]; then37cp cmd/dist/dist" $2 "38fi39mv cmd/dist/dist" $GOTOOLDIR "/ dist40exit 041fi42

4The build compiler and Go boot tool 4percent $GOHOSTOS/$GOHOSTARCH is 45echo "# Building compilers and Go bootstrap tool for host, $GOHOSTOS/$GOHOSTARCH." 4percent means to rebuild 47buildall = "- a" 4percent pass-- no-clean means not to rebuild 49if ["$1" = "--no-clean"] Then50buildall= "" 51fi52# build Go boot tool 53./cmd/dist/dist bootstrap $buildall-v # builds go_bootstrap54# Delay move of dist tool to now, because bootstrap may clear tool directory.55mv cmd/dist/dist "$GOTOOLDIR" / dist56 "$GOTOOLDIR" / go_bootstrap clean-I std57echo58

The difference between $GOHOSTARCH and $GOARCH: (the difference between $GOHOSTOS and $GOOS is the same). 6 percent GOARCH indicates what architecture the programs written by Go will run (target operating system); 6 percent GOHOSTARCH means that the system architecture running the make.bash script is generally the same, but only when cross-compilation is required. 63if ["$GOHOSTARCH"! = "$GOARCH"-o "$GOHOSTOS"! = "$GOOS"]; even if then64# is cross-compiled, the native Go environment must still have 65echo "# Building packages and commands for host, $GOHOSTOS/$GOHOSTARCH." 66GOOS=$GOHOSTOS GOARCH=$GOHOSTARCH\ 67 "$GOTOOLDIR" / go_bootstrap install-gcflags "$GO_GCFLAGS"-ldflags "$GO_LDFLAGS"-v std68echo69fi70

71echo "# Building packages and commands for $GOOS/$GOARCH." 72 "$GOTOOLDIR" / go_bootstrap install-gcflags "$GO_GCFLAGS"-ldflags "$GO_LDFLAGS"-v std73echo74

75rm-f "$GOTOOLDIR" / go_bootstrap76

7whether or not to print a message about whether the installation was successful. Generally speaking: 7 percent Installed Go for $GOOS/$GOARCH in $GOROOT79# Installed commands in $GOROOT/bin80if ["$1"! = "--no-banner"]; then81 "$GOTOOLDIR" / dist banner82fi is all the contents of this article "how to install GO in source code". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report