跳至主要内容

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

[Go] Get started with Go

Get started​

Helo World​

// hello.go
package main

import "fmt"

func main() {
fmt.Println("hello world")
}

Go CLI 執行的切入點 package main 和 func main(), 否則會報錯如下.

$ go run ./main.go
package command-line-arguments is not a main package

$ go run ./main.go
# command-line-arguments
runtime.main_main·f: function main is undeclared in the main package

External package​

$ go mod init example/hello
go: creating new go.mod: module example/hello

$ cat go.mod
module example/hello

go 1.18
package main

import (
"example/hello/foo"
"fmt"
)

func main() {
fmt.Println(foo.Foo())
}
package foo

func Foo() string {
return "this is foo package unfer hello"
}
$ go run ./main.go
this is foo func

relative path​

Go 同資料夾內所有檔案屬於相同 package, 若要拆成不同 package 必須放置不同資料夾.
在 Go Module 正式導入後, Go 不再支援相對路徑引用 package, package 需放在 Module 之下, 透過 Module 匯入. 直接夠過相對路徑匯入 package 會報錯如下:

$ go run ./main.go
main.go:4:2: "./foo" is relative, but relative import paths are not supported in module mode

Environment​

GOPATH​

...TBD...

IDE Setting​

Goland​

...TBD...

VS Code​

...TBD...

小結​

從頭開始了解語言有個明顯好處. 過去在公司開發維護專案時, 有時會太過於 follow 專案 SOP, 以及相關 Scripts, 即使這些 SOP scripts 是自己編寫維護, 但長期下來有些腳本內容僅為了解決當下問題而修改, 忽略了語言背後基礎的規範等. 而自己過於習慣使用這些腳本, 也忘了如何 From scratch.

重新了解語言基礎規範後, 也許日後要再製作 SCP scripts, 會更清楚自己在做些什麼, 清楚必要與不必要, 寫出更完善的 SOP scripts.

See Also​

Get Started​

Module​