[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
本文介绍了 Go 语言(Golang)的基本特性及其在现代软件开发中的应用,强调了 Go 的简洁性、高效性和并发处理能力。文中还概述了 Go 语言的结构(如数组、切片、结构体)、通道、并发控制等关键概念,以及相关的开发工具和框架。资料来源包括多个外部链接和参考文献,辅助读者更深入了解 Go 语言及其生态系统。
#include <stdio.h> #include <stdlib.h> intmain() { int *arr; int i; arr = malloc( 10 * sizeof(int) ); for (i = 0; i < 10; i++) { arr[i] = i; } for (i = 0; i < 10; i++) { printf("%d ", arr[i]); } free(arr); return 0; } package main import "fmt" func main() { var arr [10]int for i := 0; i < 10; i++ { arr[i] = i } for i := 0; i < 10; i++ { fmt.Printf("%d ", arr[i]) } } Golang C Array(陣列)
func (p *Car)SetYear(year int) { p.Year = year } func (p *Car) SetYear(year int) { (*p).Year = year } 同義 ⾃動解除參照(語法糖) type Car struct { Brand string Year int }
⽤ range 讀取channel •⽤ range 讀取 channel 直到它被關閉 ,若無值可取就會「等待」 •channel 關閉後,進⼊ 唯讀 狀態 •for range 會讀完 所有值 才結束 ch := make(chan int) // ... do something for i := range ch { fmt.Println(i) }
func distributedSum(numOfWorkers int,from int, to int) int { wg := &sync.WaitGroup{} wg.Add(numOfWorkers) in := make(chan int, (to-from)-1) out := make(chan int, numOfWorkers) res := make(chan int, 1) for i := 0; i < numOfWorkers; i++ { go sumWorker(in, out, wg) } go compilationWorker(out, res) for i := 0; i <= to; i++ { in <- i // 塞資料給各個 sumWorkers } close(in) // 關閉資料窗⼝ (通知各個 sumWorkers 停⽌讀值) wg.Wait() // 等待所有 workers 結束 close(out) // 關閉資料窗⼝ (通知 compilationWorker 停⽌讀值) return <-res }
42.
func sumWorker(in chanint, out chan int, wg *sync.WaitGroup) { defer wg.Done() num := 0 for n := range in { // 讀取通道直到通道被關閉 num += n time.Sleep(time.Millisecond * 30) } fmt.Printf("partial sum: %dn", num) out <- num } func compilationWorker(in chan int, out chan int) { sum := 0 for i := range in { // 讀取通道直到通道被關閉 sum += i } out <- sum }
43.
•完全⾃學!Go 語⾔ (Golang)實戰聖經 (The Go Workshop: Learn to write clean, efficient code and build high-performance applications with Go) https://www.tenlong.com.tw/products/9789863126706 •下班加減學點 Golang 與 Docker https://ithelp.ithome.com.tw/users/20104930/ironman/2647 •被選召的 Gopher 們,從零開始探索 Golang, Istio, K8s 數碼微服務世界 https://ithelp.ithome.com.tw/articles/10240228 •PJCHENder 未整理筆記 https://pjchender.dev/golang/go-getting-started/ 參考資料
44.
Mobile API CRUD Login / Logout Token(Session) JWT / Clean Architecture Unit Test Docker Kubernetes (k8s) Load Balancer Database Cache Queue Swagger channel mutex lock CI / CD MongoDB RDBMS MySQL PostgreSQL Memcached Redis RabbitMQ grafana + prometheus Pod Service Ingress SOLID 5 OOP 3 goroutine Backup DRP (Disaster Recovery Plan) Logs BCP (Business Continuity Plan) Git Logger
45.
obile API CRUD Login / Logout Token(Session) JWT / Clean Architecture Unit Test Docker Kubernetes (k8s) Swagger channel mutex lock Pod Service Ingress SOLID 5 OOP 3 goroutine