這是小弟第一次參加鐵人賽, 來挑戰一下自我.
開始學著寫Golang的原因是因為寫了幾年NodeJS跟C#,
但Node真的一個專案打包成docker image超臃腫.
就嘗試找一個也支援高併發, 性能優, 方便部屬的語言,
但希望它的執行檔大小能是超小的, 且各種OS都支援.
就選擇Golang這語言了.
就下班加減學一點學一點, 至今也看了兩三個月.
一些東西紀錄在自己的部落格當作筆記
NodeJS
console.log("hello world");
> node app.js
Golang的對等寫法
package main
import (
"fmt"
)
func main() {
fmt.Println("hello world")
}
> go run main.go
const names = ["it", "home"];
names := []string { "it", "home"}
let game = "it home iron man";
console.log(game.substr(8, game.length));
game := "it home iron man"
fmt.Println(game[8: ])
const gender = 'female';
switch (gender) {
case 'female':
console.log("you are a girl");
break;
case 'male':
console.log("your are a boy");
break;
default:
console.log("wtf");
}
gender := "female"
switch gender {
case "female":
fmt.Println("you are a girl")
case "male":
fmt.Println("your are a boy")
default:
fmt.Println("wtf")
}
看得出來Go省略了break這關鍵字
Javascript有for loop, while loop, do while loop
Go只有for loop 就能模擬上面三個
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",
}
for key, value := range kvs {
fmt.Println(key, value)
}
const Post = {
ID: 10213107
Title: "下班加減學點Golang",
Author: "Nathan",
Difficulty: "Beginner",
}
type Post struct {
ID int
Title string
Author string
Difficulty string
}
p := Post {
ID: 10213107,
Title : "下班加減學點Golang",
Author: "Nathan",
Difficulty:"Beginner",
}
Go能透過定義抽象的struct與其屬性, 在實例化
也能透過map[string]interface來定義
Post := map[string]interface{} {
"ID": 10213107,
"Title" : "下班加減學點Golang",
"Author": "Nathan",
"Difficulty":"Beginner",
}
從上面幾個例子就能看的出來Node跟Go語法結構上很類似,
所以學過Node再來學Go好像就沒那麼難了 XD
之後會慢慢補充Go的更多東西.
謝謝各位
go跟JS一點都不像啊...
比較像C,畢竟他也是號稱21世紀的C。
最近也在學go。
應該說
我之前寫Node 所以會以node的例子再思考跟go有哪些相似的部分