Golang 101&指令

Golang 101

网址 & 相应视频

Golang 指令

Go is a tool for managing Go source code.

Usage:

        go command [arguments]

The commands are:

        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        bug         start a bug report
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         download and install packages and dependencies
        install     compile and install packages and dependencies
        list        list packages
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help [command]" for more information about a command.

Additional help topics:

        c           calling between Go and C
        buildmode   build modes
        cache       build and test caching
        filetype    file types
        gopath      GOPATH environment variable
        environment environment variables
        importpath  import path syntax
        packages    package lists
        testflag    testing flags
        testfunc    testing functions

Use "go help [topic]" for more information about that topic.
# 编译运行helloworld.go,不保存编译得到的可执行二进制文件
go run helloworld.go
# go get 从指定源上面下载或者更新指定的代码和依赖,并对他们进行编译和安装
# go get = git clone + go install
go get
# 编译helloworld.go得到可执行二进制文件helloworld
go build helloworld.go
# 运行可执行二进制文件helloworld
./helloworld
# go build & go install 见下面

go build vs go install

go build -x以及go install -x会把执行细节都打印出来。

二者指向的默认都是当前目录上的package(并不会递归进入当前目录下的其他文件夹),若当前目录有两个不同的package,则会引起一下错误,如can't load package: package helloworld: found packages helloworld (gen_hello.go) and main (main.go) in E:\Go\myGo\src\helloworld

go install & go build都可以指定相应的包名(可带路径)(非文件名)。当然go install main & go install main都会出错,正确的是go install <main包所在的目录> & go install <main包所在的目录>

区别

  • For packages

    • go build 编译当前package以及其依赖,但不生成任何东西,只是用来看看有没有编译错误
    • go install 编译档期package以及其依赖,并在 $GOPATH/pkg下生成当前package的.a临时文件(依赖目录下不会生成.a文件)
  • For commands (package main)

    • go build 重新编译所有的依赖以及main包,并在当前目录生成可执行文件
    • go install 首先观察依赖目录下是否有相应的.a文件,若有直接用,若无会编译该依赖包(但不会生成.a文件);然后编译main包并在 $GOPATH/bin($GOBIN)目录下生成可执行文件。

.a文件有什么用?

包x的.a文件只有在本包目录下执行go install才能生成。如果想相应的依赖都生成.a文件,可以用go install -i

.a文件目的是免得每次都从源码重新编译,加快整个项目的编译过程。
一个例子:
On my opinion, I prefer use go install, because my project has 50k lines of code. It takes 1min to build by using go build even I only change a file. When I use go install, it will take the same time with go build the first time, and I only take 4 sec to recompile if I change a file(most time spend on the linker).

**这里会有个坑,如果你跟新了包的源码,但没有重新生成.a文件那么,go install的过程仍然用原来的.a文件,若以源码修改无效。但go build不会有这个问题。**解决方法:

  1. go install -a强制更新所有的依赖包,包括Go内置的包。这个方案最简单可靠,不过编译时间会稍长。
  2. 直接go install被修改过的包

猜你喜欢

转载自blog.csdn.net/jason_cuijiahui/article/details/84636594