About the go command - go.dev
文章推薦指數: 80 %
For example, the source code of the Google logging package glog is obtained by running ... We can install both with the " go get " subcommand:
WhyGo
GetStarted
Docs
Packages
Play
Blog
Aboutthegocommand
TheGodistributionincludesacommand,named
"go",that
automatesthedownloading,building,installation,andtestingofGopackages
andcommands.Thisdocumenttalksaboutwhywewroteanewcommand,whatit
is,whatit'snot,andhowtouseit.
Motivation
YoumighthaveseenearlyGotalksinwhichRobPikejokesthattheidea
forGoarosewhilewaitingforalargeGoogleservertocompile.That
reallywasthemotivationforGo:tobuildalanguagethatworkedwell
forbuildingthelargesoftwarethatGooglewritesandruns.Itwas
clearfromthestartthatsuchalanguagemustprovideawayto
expressdependenciesbetweencodelibrariesclearly,hencethepackage
groupingandtheexplicitimportblocks.Itwasalsoclearfromthe
startthatyoumightwantarbitrarysyntaxfordescribingthecode
beingimported;thisiswhyimportpathsarestringliterals.
AnexplicitgoalforGofromthebeginningwastobeabletobuildGo
codeusingonlytheinformationfoundinthesourceitself,not
needingtowriteamakefileoroneofthemanymodernreplacementsfor
makefiles.IfGoneededaconfigurationfiletoexplainhowtobuild
yourprogram,thenGowouldhavefailed.
Atfirst,therewasnoGocompiler,andtheinitialdevelopment
focusedonbuildingoneandthenbuildinglibrariesforit.For
expedience,wepostponedtheautomationofbuildingGocodebyusing
makeandwritingmakefiles.Whencompilingasinglepackageinvolved
multipleinvocationsoftheGocompiler,weevenusedaprogramto
writethemakefilesforus.Youcanfinditifyoudigthroughthe
repositoryhistory.
Thepurposeofthenewgocommandisourreturntothisideal,thatGo
programsshouldcompilewithoutconfigurationoradditionalefforton
thepartofthedeveloperbeyondwritingthenecessaryimport
statements.
Configurationversusconvention
Thewaytoachievethesimplicityofaconfiguration-freesystemisto
establishconventions.Thesystemworksonlytotheextentthatthoseconventions
arefollowed.WhenwefirstlaunchedGo,manypeoplepublishedpackagesthat
hadtobeinstalledincertainplaces,undercertainnames,usingcertainbuild
tools,inordertobeused.That'sunderstandable:that'sthewayitworksin
mostotherlanguages.Overthelastfewyearsweconsistentlyremindedpeople
aboutthegoinstallcommand
(nowreplacedbygoget)
anditsconventions:first,thattheimportpathisderivedinaknownwayfrom
theURLofthesourcecode;second,thattheplacetostorethesourcesin
thelocalfilesystemisderivedinaknownwayfromtheimportpath;third,
thateachdirectoryinasourcetreecorrespondstoasinglepackage;and
fourth,thatthepackageisbuiltusingonlyinformationinthesourcecode.
Today,thevastmajorityofpackagesfollowtheseconventions.
TheGoecosystemissimplerandmorepowerfulasaresult.
Wereceivedmanyrequeststoallowamakefileinapackagedirectoryto
providejustalittleextraconfigurationbeyondwhat'sinthesourcecode.
Butthatwouldhaveintroducednewrules.Becausewedidnotaccedetosuch
requests,wewereabletowritethegocommandandeliminateouruseofmake
oranyotherbuildsystem.
Itisimportanttounderstandthatthegocommandisnotageneral
buildtool.Itcannotbeconfiguredanditdoesnotattempttobuild
anythingbutGopackages.Theseareimportantsimplifying
assumptions:theysimplifynotonlytheimplementationbutalso,more
important,theuseofthetoolitself.
Go'sconventions
Thegocommandrequiresthatcodeadherestoafewkey,
well-establishedconventions.
First,theimportpathisderivedinaknownwayfromtheURLofthe
sourcecode.ForBitbucket,GitHub,GoogleCode,andLaunchpad,the
rootdirectoryoftherepositoryisidentifiedbytherepository's
mainURL,withoutthehttps://prefix.Subdirectoriesarenamedby
addingtothatpath.
Forexample,thesourcecodeoftheGoogleloggingpackageglog
isobtainedbyrunning
gitclonehttps://github.com/golang/glog
andthustheimportpathofthe
glog
packageis"github.com/golang/glog".
Thesepathsareonthelongside,butinexchangewegetan
automaticallymanagednamespaceforimportpathsandtheabilityfor
atoollikethegocommandtolookatanunfamiliarimportpathand
deducewheretoobtainthesourcecode.
Second,theplacetostoresourcesinthelocalfilesystemisderived
inaknownwayfromtheimportpath,specifically
$GOPATH/src/
延伸文章資訊
- 1go get - GO 命令教程- 极客学院Wiki
命令 go get 可以根据要求和实际情况从互联网上下载或更新指定的代码包及其依赖包,并对它们进行编译和安装。在上面这个示例中,我们从著名的代码托管站点 ...
- 2How can I install a package with go get? - Stack Overflow
See also: go build, go install, go clean. For example, showing verbose output, $ go get -v github...
- 3gotypes - example - Git at Google
Later examples will be variations on this one, and we'll often omit boilerplate details such as p...
- 4go command - cmd/go - pkg.dev
go get example.com/mod@none. See https://golang.org/ref/mod#go-get for details. The 'go install' ...
- 5"go get" command
go get " command is the standard way of downloading and installing packages and related ... and l...