src - Contains the source files for your own or other downloaded packages. You can build and run
programs while in a program directory under this directory.
pkg - Contains compiled package files. Go uses this to make the builds (compilation & linking) faster.
bin - Contains compiled and executable Go programs. When you call go install on a program
directory, Go will put the executable under this folder.
Golang Example
12345678910111213
// 宣告程式屬於哪個 package,所有的 go 檔案都必須聲明packagemain// 引入套件,多個可以加括號 ()import"fmt"// 程式執行入口,main 在 golang 中是特殊的 function,每個執行檔只能有一個,告訴 Golang 要從哪裡開始執行funcmain(){// 使用 fmt 套件印出字串 hello worldfmt.Println("hello world")}// Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.
go run main.go 就可以直接執行
另外也可以先 build 產生執行檔於當前目錄
在該目錄跑 go build main.go 後執行 ./main
package
12
// package clausepackagetest
宣告程式屬於哪個 package,所有的 go 檔案都必須聲明,要 import 這個檔案時,就必須使用 test 這個名稱。
而 go 又分兩種專案
執行檔 (executable)
created for running
name should be main
always func main
函式庫 (library)
created for reusability
can have any name
no function main
執行檔一定要宣告為 main 套件
12
// 沒聲明 main 會顯示gorun:cannotrunnon-mainpackage
main 是一個比較特殊的 package,
Package main is special. It defines a standalone executable program, not a library. Within package main the function main is also special—it’s where execution of the program begins. Whatever main does is what the program does.
packagemainimport("fmt")funcmain(){fmt.Printf("%s, %s and %[2]v, %[1]v\n","Hello","world")fmt.Printf("%f, %.[1]f, %.2[1]f",123.123)}// Hello, world and world, Hello// 123.123000, 123, 123.12
特殊關鍵字 && 內建類別 && 內建函示
123456789101112131415161718192021
// 特殊關鍵字breakdefaultfuncinterfaceselectcasedefergomapstructchanelsegotopackageswitchconstfallthroughifrangetypecontinueforimportreturnvar// 內建類別-*string-*bool-NumericTypes-*intint8int16*int32(rune)int64-uint*uint8(byte)uint16uint32uint64-float32*float64-complex64complex128-byte// alias for uint8-rune// alias for int32,represents a Unicode code point// 內建函示makelencapnewappendcopyclosedeletecomplexrealimagpanicrecover
// = 使用必須使用先var聲明vara// 不定型別的變數varaint// 宣告成 intvaraint=10// 初始化同時宣告vara,bint// a 跟 b 都是 int,沒有給值 int 預設是 0vara,bint=100,50// 同時宣告一樣 type 並給值vara=10// 自動推斷型別vara,b=0,"test"// 同時宣告自動判斷 type (必須是同時給值)var(abool=falsebintc="hello")// 多個同時宣告和給值,可以用括號包再一起 (不能同行,不然會錯)
Short hand declaration
12345678910111213141516171819202122232425
// := 是聲明並賦值,並且系統自動推斷類型,必須放在 main function 裡面// := only works in functions and the lower case 't' is so that it is only visible to the package (unexported).a:=0a,b,c:=0,true,"test"a,b:=1,2b,c:=3,4// 重複宣告,因為 c 是新的變數,因此可以通過fmt.Println(a,b,c)// 1, 3, 4a,b:=1,2a,b:=3,4// 重複宣告,因為沒有新的參數,因此會報錯fmt.Println(a,b)// no new variables on left side of :=a,b:=1,2a,b=3,4// 這邊是用 = 不是 := 因此是 assign 新的值fmt.Println(a,b)age:=29age="naveen"// golang 是強型別,一但定義就無法轉換成其他型別// cannot use "test" (type string) as type int in assignmentsum:=float64(0)// 宣告並給值
redeclaration
12345678910111213
packagemainimport("fmt")funcmain(){a,b:=1,2// 同時進行 declare + assign// 一定要有一個 new declare 否則會 no new variables on left side of :=a,c:=3,4fmt.Println(a,b,c)}
常數 Constants
constants 常用於用固定常數,必須一開始就 initialized
1234567891011121314151617181920212223
packagemainimport"fmt"funcmain(){constPi=3.14constWorld="世界"const(// Create a huge number by shifting a 1 bit left 100 places.// In other words, the binary number that is 1 followed by 100 zeroes.Big=1<<100// Shift it right again 99 places, so we end up with 1<<1, or 2.Small=Big>>99)fmt.Println("Pi",Pi)fmt.Println("World",World)//fmt.Println(Big)fmt.Println(Small)}// Pi 3.14// World 世界// 2
constants 宣告後就不能重複 asssign (immutable)
You cannot change constant values (immutable)
123456
packagemainfuncmain(){consta=55//alloweda=89//reassignment not allowed}
constants 在編譯的時候就要定義,不能在執行時才給值
You cannot initialize constant to a runtime value
1234567891011121314151617181920212223
packagemainimport("fmt""math")funcmain(){fmt.Println("Hello, playground")vara=math.Sqrt(4)//allowed// 因為 math.Sqrt(4) 是在 runtime 才會知道值,所以會 errorconstb=math.Sqrt(4)// not allowedc:=2constd=c// not allowedconste=len("abc")// ok,因為在 compile time 就可以知道值,而不是 runtimefmt.Println(e)constf=123// compile time 就可以知道constg=ffmt.Println(g)}
const 沒有特別聲明是哪種型態時,是屬於 typeless
當 const assign 給任意 type 的值時,會由 typeless 會自動轉變為該 type,而 var 一開始就會給定 type,因此會 error
packagemainimport("fmt")funcmain(){constmin=43// typelessvaraint=min// min's type = int, 相當於 var a = int(min)varbfloat64=min// min's type = float64, 相當於 var a = float64(min)varcbyte=min// min's type = byte, 相當於 var a = byte(min)vardrune=min// min's type = rune, 相當於 var a = rune(min)fmt.Println(a,b,c,d)}
多重宣告 const,後面沒有給值,會跟前面的一樣
Constants get their types and expressions from the previous constant
12345678910111213141516
packagemainimport("fmt")funcmain(){const(maxint=123min// it repeats the previous constant's type and/or expression)fmt.Println(max,min)}// 123 123