package mainimport "fmt"import "os"var (a intb int)func testArrayParmter(arr [2]int) {//arr傳進來是個副本,所以函數內改變arr,對外部不影響println("")for i := range arr {arr[i] += 1print(arr[i])if !(i+1 == len(arr)) {print(",")}}println("")}func testSliceParmter(arr []int) {println("")for i := range arr {arr[i] += 1print(arr[i])if !(i+1 == len(arr)) {print("^,")}}println("")}func main() {for i := 0; i < 5; i++ {for j := 0; j <= i; j++ {print("A")}println("")}a = 0x77b = 077const c = "i am const var"const (d = iotaef = iota)s := "hello" + " world"ch := []byte(s) //將s轉成位元組數組ch[0] = 'c's2 := string(`ssfswerq`)if 1 < 2 {if 1 > 0 && !false {fmt.Printf("0<1<2\n")} else {fmt.Printf("not 0<1<2\n")}}var cc complex128 = 5 + 5ifmt.Printf("d=%d e=%d f=%d s2=%s cc=%v\n", d, e, f, s2, cc)fmt.Printf("s=%s a=%d b=%d c=%s\n", s, a, b, c)file, err := os.OpenFile("c:\\2.py", os.O_RDONLY, 0)if err != nil {fmt.Println(err)return}by := []byte(s)file.Read(by)fmt.Println(string(by)) //讀取一行dd, err := file.Stat()if err != nil {fmt.Println(err)return}fmt.Println(dd)file.Close()//3種for的使用for i := 1; i < 2; i += 1 {fmt.Println(i)}i := 2flag := trueHere: //goto的使用for i >= 0 {fmt.Println(i)i -= 1}for {println("while True{}")break}if flag {flag = falsegoto Here}j := 0J:for i = 0; i < 100; i++ {for j = 0; j < 100; j++ {if i > 5 {break J //跳出J標籤所在的迴圈層}}}println(i, j)println("====range的使用====")list := []string{"a", "b", "c"}for k, v := range list {println(k, v)}for pos, cha := range "abc" {fmt.Printf("#pos=%d cha='%c'\n", pos, cha) //為何這個會跑到while True{}前面輸出}println("switch的使用")ccc := 'A'switch { //如果switch沒有運算式,它會匹配true//轉化為一個個匹配,知道為truecase '0' <= ccc && ccc <= '9':println('0')case 'a'-32 <= ccc && ccc <= 'f'-32:println('a')case 'A' <= ccc && ccc <= 'F': //不會執行println('A')}switch j {case 1:println("case 1")case 2:println("case 2")default:println("case default")}ch = []byte("hhhhhh")ch2 := []byte("hhhhhhh")println(len(ch), len(ch2))for pos := range ch { //比較字串大小switch {case ch[pos] > ch2[pos]:println("big")case ch[pos] < ch2[pos]:println("small")}}println("數組的使用")arr := [2]int{1, 2} //這兩個數組都初始化為1,2var arr2 [2]intarr2 = arr //數組同樣是實值型別的:將一個數組賦值給另一個數組,會複製所有的元素for i := range arr2 {print(arr2[i])}testArrayParmter(arr)for i := range arr2 {print(arr[i])}println("")arr3 := [...]int{1, 2, 3, 4, 5, 6, 7, 8}println(len(arr3))println("數組的建立")a3 := [3][2]int{[2]int{1, 2}, [...]int{1, 2}, {3, 4}}println(len(a3))println("slice的使用---slice是指向數組頭部的指標,且是參考型別,要用make建立")sl := make([]int, 10) //建立了一個儲存有10個元素的slicesl[1] = 2println(sl[1])sl2 := arr3[2:4]println(sl2[0], sl2[1], len(sl2), arr3[:len(arr3)])println("使用內建函數append和copy")sa := []int{0, 0}sb := append(sa, 2, 2)sc := append(sb, sa...) //{0,0,2,2,0,0}for i := 0; i < len(sc); i++ { //會跑到goto那去fmt.Printf("=%d=", sc[i])}println("")var a4 = [8]int{0, 1, 2, 3, 4, 5, 6, 7}var ss = make([]int, 6)//copy最多複製的元素是兩個參數數組的最小長度n1 := copy(ss, a4[0:]) //因為s的長度為6,故此時s=={0,1,2,3,4,5}n2 := copy(ss, ss[2:]) //由於複製重疊了,故此時s=={2,3,4,5,4,5}println(n1, n2)testSliceParmter(ss) //返回後ss=={3,4,5,6,5,6}println("map的使用")monthdays := map[string]int{"Jan": 31, "Feb": 28, "Mar": 31,"Apr": 30, "May": 31, "Jun": 30,"Jul": 31, "Aug": 31, "Sep": 30,"Oct": 31, "Nov": 30, "Dec": 31, //注意最後一個逗號不能少}println(monthdays["Nov"])year := 0for _, days := range monthdays {year += days}monthdays["ckl_day"] = 31 //向map添加新項val, ok := monthdays["ckl_day"] //判斷monthdays["ckl_day"]是否存在,儲存在ok中//刪除monthdays["ckl_day"]項delete(monthdays, "ckl_day")year2 := 0for _, days := range monthdays {year2 += days}val1, ok1 := monthdays["ckl_day"]println(year, monthdays["ckl_day"], val, ok, year2, val1, ok1)}