Go run
[root@localhost word]# vim demo.go
[root@localhost word]# ls
demo.go
[root@localhost word]# go run demo.go
hello,word!
[root@localhost word]# vim hello.go
[root@localhost word]# go run hello.go --filename hello.go
# command-line-arguments
./hello.go:16:21: syntax error: unexpected os, expecting comma or )
Go install
[root@localhost word]# go run demo.go
在~/.Bashrc中编辑如图最后一条命令
[root@localhost day02]# vim main.go
[root@localhost day02]# ls
demo.go goinstall hello.go main.go
[root@localhost day02]# go install -x main.go
WORK=/tmp/go-build685806351
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg << 'EOF' # internal
# import config
packagefile fmt=/usr/local/go/pkg/linux_amd64/fmt.a
packagefile runtime=/usr/local/go/pkg/linux_amd64/runtime.a
EOF
cd /root/a/day02
/usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/b001/_pkg_.a -trimpath "$WORK/b001=>" -p main -complete -buildid nASZ5Ii-_fTYh7rIMpez/nASZ5Ii-_fTYh7rIMpez -goversion go1.18.4 -c=4 -nolocalimports -importcfg $WORK/b001/importcfg -pack ./main.go
/usr/local/go/pkg/tool/linux_amd64/buildid -w $WORK/b001/_pkg_.a # internal
cp $WORK/b001/_pkg_.a /root/.cache/go-build/34/344bdfd5537e4daaf0f1359f993333d26f7c904b0acea18db439d8e516739bc5-d # internal
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile command-line-arguments=$WORK/b001/_pkg_.a
packagefile fmt=/usr/local/go/pkg/linux_amd64/fmt.a
packagefile runtime=/usr/local/go/pkg/linux_amd64/runtime.a
packagefile errors=/usr/local/go/pkg/linux_amd64/errors.a
packagefile internal/fmtsort=/usr/local/go/pkg/linux_amd64/internal/fmtsort.a
packagefile io=/usr/local/go/pkg/linux_amd64/io.a
packagefile math=/usr/local/go/pkg/linux_amd64/math.a
packagefile os=/usr/local/go/pkg/linux_amd64/os.a
packagefile reflect=/usr/local/go/pkg/linux_amd64/reflect.a
packagefile strconv=/usr/local/go/pkg/linux_amd64/strconv.a
packagefile sync=/usr/local/go/pkg/linux_amd64/sync.a
packagefile unicode/utf8=/usr/local/go/pkg/linux_amd64/unicode/utf8.a
packagefile internal/abi=/usr/local/go/pkg/linux_amd64/internal/abi.a
packagefile internal/bytealg=/usr/local/go/pkg/linux_amd64/internal/bytealg.a
packagefile internal/cpu=/usr/local/go/pkg/linux_amd64/internal/cpu.a
packagefile internal/goarch=/usr/local/go/pkg/linux_amd64/internal/goarch.a
packagefile internal/goexperiment=/usr/local/go/pkg/linux_amd64/internal/goexperiment.a
packagefile internal/goos=/usr/local/go/pkg/linux_amd64/internal/goos.a
packagefile runtime/internal/atomic=/usr/local/go/pkg/linux_amd64/runtime/internal/atomic.a
packagefile runtime/internal/math=/usr/local/go/pkg/linux_amd64/runtime/internal/math.a
packagefile runtime/internal/sys=/usr/local/go/pkg/linux_amd64/runtime/internal/sys.a
packagefile runtime/internal/syscall=/usr/local/go/pkg/linux_amd64/runtime/internal/syscall.a
packagefile internal/reflectlite=/usr/local/go/pkg/linux_amd64/internal/reflectlite.a
packagefile sort=/usr/local/go/pkg/linux_amd64/sort.a
packagefile math/bits=/usr/local/go/pkg/linux_amd64/math/bits.a
packagefile internal/itoa=/usr/local/go/pkg/linux_amd64/internal/itoa.a
packagefile internal/oserror=/usr/local/go/pkg/linux_amd64/internal/oserror.a
packagefile internal/poll=/usr/local/go/pkg/linux_amd64/internal/poll.a
packagefile internal/syscall/execenv=/usr/local/go/pkg/linux_amd64/internal/syscall/execenv.a
packagefile internal/syscall/unix=/usr/local/go/pkg/linux_amd64/internal/syscall/unix.a
packagefile internal/testlog=/usr/local/go/pkg/linux_amd64/internal/testlog.a
packagefile internal/unsafeheader=/usr/local/go/pkg/linux_amd64/internal/unsafeheader.a
packagefile io/fs=/usr/local/go/pkg/linux_amd64/io/fs.a
packagefile sync/atomic=/usr/local/go/pkg/linux_amd64/sync/atomic.a
packagefile syscall=/usr/local/go/pkg/linux_amd64/syscall.a
packagefile time=/usr/local/go/pkg/linux_amd64/time.a
packagefile unicode=/usr/local/go/pkg/linux_amd64/unicode.a
packagefile internal/race=/usr/local/go/pkg/linux_amd64/internal/race.a
packagefile path=/usr/local/go/pkg/linux_amd64/path.a
modinfo "0w\xaf\f\x92t\b\x02A\xe1\xc1\a\xe6\xd6\x18\xe6path\tcommand-line-arguments\nbuild\t-compiler=gc\nbuild\tCGO_ENABLED=1\nbuild\tCGO_CFLAGS=\nbuild\tCGO_CPPFLAGS=\nbuild\tCGO_CXXFLAGS=\nbuild\tCGO_LDFLAGS=\nbuild\tGOARCH=amd64\nbuild\tGOOS=linux\nbuild\tGOAMD64=v1\n\xf92C1\x86\x18 r\x00\x82B\x10A\x16\xd8\xf2"
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/go/pkg/tool/linux_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=zG7x_9fTw2UubHnmP7EW/nASZ5Ii-_fTYh7rIMpez/K_o-qPvbyQi5WMsA95S6/zG7x_9fTw2UubHnmP7EW -extld=gcc $WORK/b001/_pkg_.a
/usr/local/go/pkg/tool/linux_amd64/buildid -w $WORK/b001/exe/a.out # internal
mkdir -p /usr/local/bin/
mv $WORK/b001/exe/a.out /usr/local/bin/main
rm -r $WORK/b001/
[root@localhost day02]# cd /usr/local/bin
[root@localhost bin]# ls
demo main
[root@localhost bin]# main
hello,word!
Go build
root@localhost mypkg]# ls
mypkg.go
[root@localhost mypkg]# cd ../
[root@localhost goinstall]# ls
main.go mypkg
[root@localhost goinstall]# vim main.go
[root@localhost goinstall]# cd ../
[root@localhost day02]# go build -o main goinstall
/usr/local/go/src/goinstall/main.go:12:2: package goinstall/mypakg is not in GOROOT (/usr/local/go/src/goinstall/mypakg)
[root@localhost day02]# cp goinstall /usr/local/go/src/
cp: -r not specified; omitting directory 'goinstall'
[root@localhost day02]# cp goinstall /usr/local/go/src/ -r
cp: overwrite '/usr/local/go/src/goinstall/main.go'? y
cp: overwrite '/usr/local/go/src/goinstall/mypkg/mypkg.go'? y
[root@localhost day02]# go build -o main install
package install is not in GOROOT (/usr/local/go/src/install)
[root@localhost day02]# go build -o main goinstall
[root@localhost day02]# ./ main
-bash: ./: Is a directory
[root@localhost day02]# ls
demo.go goinstall hello.go main main.go
[root@localhost day02]# ./ main
-bash: ./: Is a directory
[root@localhost day02]# ./main
call CustomPkgFunc
hello,wo de