看看這個shell會話,我在Go中創建一個簡單的hello world程序。
$ cd ~/lab/hello/
$ ls
hello.go
$ cat hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
$ go build
$ ./hello
hello, world
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.7 (jessie)
Release: 8.7
Codename: jessie
這是我不明白。教程https://golang.org/doc/install#testing說我應該把我的hello.go
文件放在〜/ go/src/hello。但我沒有遵循這一點。我的程序如何編譯呢?如果我的程序以這種方式編譯,那麼爲什麼文檔說我應該保留我的源代碼在〜/ go/src或$ GOPATH/src中,這似乎不重要?
有沒有一種情況下,真的有必要將源代碼放在$ GOPATH/src?
嘗試[創建一個庫](https://golang.org/doc/code.html#Library)。 –
閱讀[如何寫代碼](https://golang.org/doc/code.html),它解釋了所有這些。 – JimB
@MadWombat遵循經過測試,驗證,推薦的方式「有些愚蠢」?如果一臺機器的設計者告訴你:「你必須先按這個按鈕,然後你才能做X.」你是否還會問有什麼證據可以首先按下按鈕? 「工具的創造者告訴你這樣做」沒有足夠的證據?那麼,可能不是自我在編程方面那麼強大。 – Volker