package main
import ("fmt")
func main() {
//## Hello world
fmt.Println("Hello world. 哈囉世界好。")
//## Array 和 Sllice
names := []string {"it","home"}
fmt.Println(names)
//## 印出後面幾個字的子字串
game := "it home iron man"
fmt.Println(game[8:])
//## 流程控制
gender := "female"
// if syntax
if gender == "male" {
fmt.Println("我是男生")
} else {
fmt.Println("我不是男生")
}
// switch syntax, 其中沒有 break 關鍵字
switch gender {
case "female":
fmt.Println("you are a girl.")
case "male":
fmt.Println("you are a boy.")
default:
fmt.Println("you arx a X-man. ")
}
//## Loop
// for-loop syntax
for i:=0; i<10; i++ {
fmt.Println(i)
}
// key value pairs
kvs := map[string]string {
"name": "it home",
"website": "https://ithelp.ithome.com.tw",
}
// foreach syntax
for key,value := range kvs {
fmt.Println(key, value)
}
//## Object
// 用字首大小寫, 判別是否是public / private
type Post struct {
ID int
Title string
Author string
Difficulty string
}
p := Post {
ID: 12345678,
Title: "上班不要混",
Author: "Rely",
Difficulty: "Beginner",
}
fmt.Println(p)
fmt.Println(p.Author)
// 用 map[key]interface 語法
// 效果等同 anonymous type
p2 := map[string]interface{} {
"ID": 87654321,
"Title" : "下班加減學點Golang",
"Author": "Nobody",
"Difficulty":"Beginner",
}
fmt.Println(p2)
fmt.Println(p2["Author"])
}h