Golang基本命令詳解

2021-10-11 13:22:49 字數 3256 閱讀 5149

c:\users\zhang>go --help

go is a tool for managing go source code.

usage:

go [arguments]

其中 command 是操作命令 arg是該命令的引數,像:go run hello.go

the commands are:

bug start a bug report

build compile packages and dependencies

clean remove object files and cached files

doc show documentation for package or symbol

env print go environment information

fix update packages to use new apis

fmt gofmt (reformat) package sources

generate generate go files by processing source

get add dependencies to current module and install them

install compile and install packages and dependencies

list list packages or modules

mod module maintenance

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 " for more information about a command.

additional help topics:

buildconstraint build constraints

buildmode build modes

c calling between go and c

cache build and test caching

environment environment variables

filetype file types

go.mod the go.mod file

gopath gopath environment variable

gopath-get legacy gopath go get

goproxy module proxy protocol

importpath import path syntax

modules modules, module versions, and more

module-get module-aware go get

module-auth module authentication using go.sum

module-private module configuration for non-public modules

packages package lists and patterns

testflag testing flags

testfunc testing functions

use "go help " for more information about that topic.

c:\users\zhang>

go build compile packages and dependencies

:編譯包和依賴項,可檢查是否存在編譯錯誤,如果被編譯的是main包,會生成可執行檔案;

:go build hello.go 只編譯單個檔案成可執行檔案;

:只有檔案中有main函式才可以生成可執行檔案,否則go build只檢查編譯錯誤,不生成可執行檔案;

:可通過 go build 編譯帶main函式的整個包形成可執行檔案

:例如:工程存放在 c:\go\src\golang_maininit_src\ 目錄下

:在 c:\go 下開啟命令列,敲入go build .\src\golang_maininit_src\,則可執行檔案生成在c:\go 目錄下;

:在 c:\go\src\golang_maininit_src\目錄下開啟命令列,敲入go build,則可執行檔案生成在c:\go\src\golang_maininit_src\目錄下;

:注意:使用go build編譯整個包,工程要在gopath環境變數裡,否則系統找不到;

go get add dependencies to current module and install them

:用於動態獲取遠端**包,並將依賴項新增到當前模組並安裝它們;

:go get github.com/mattn/go-sqlite3 預設拉到當前的 gopath目錄下,可通過go env;

run compile and run go program

:編譯和直接執行go程式,它會生成乙個臨時檔案,但不是乙個標準的可執行檔案,直接在命令列列印輸出程式執行結果,方便使用者除錯;

:只能有 main 入口函式的才可以直接通過 go run執行。

​ go fmt 格式化原始碼,有的ide儲存原始碼時自動執行該命令,比如subl,也可手動執行它

​ go install 命令的作用有兩步:第一步,編譯匯入的包檔案,所有匯入的包檔案編譯完才會編譯主程式;第二步,將編譯後生成的可執行檔案放到bin目錄下(gopath/bin),編譯後的包檔案放到pkg目錄下( gopath/pkg)

​ go test命令用於執行測試檔案,該命令會自動讀取原始碼目錄下的名為:*_test.go的檔案,生成並執行測試用的可執行檔案,測試成功會顯示「pass」、「ok」等資訊。

Vagrant基本命令詳解

vagrant version vagrant 1.8.1 vagrant box list centos 7 virtualbox,1603.01 ubuntu trusty64 virtualbox,20160406.0.0 vagrant box add address1 box名簡寫 vag...

Linux基本命令詳解

1.4 編輯linux命令列 二.linux命令幫助 三 命令講解 是整條命令中最關鍵的一部分,唯一確定一條命令。執行的是操作,如新建,刪除,重新命名等。理解為功能。短格式最常用短格式選項 使用 符號引導 多個單字元選項可以組合在一起使用。跟單個字元。優點 使用最短的字元檢視最多的資訊。如 ls a...

linux基本命令 mv詳解

mv 選項 引數 剪下,或在同目錄下移動重新命名 1.如果目標檔案是資料夾,則原始檔直接移動到該資料夾內,名字還是原始檔的名字。2.如果目標檔案時檔案,則原始檔移動的同時也會更改名字 3.如果原始檔為多個,則目標必須是目錄,並且統一移動到目錄下 b 當目標檔案存在時,先進行備份在覆蓋 root bo...