SlideShare a Scribd company logo
Introduction of GoLang
Johnny Sung
圖片來源:https://www.gctel.com/gopher-bounty-match-program/
Full stack developer
Johnny Sung (宋岡諺)
https://fb.com/j796160836
https://blog.jks.co
ff
ee/
https://www.slideshare.net/j796160836
https://github.com/j796160836
https://www.facebook.com/ProgrammersCreateLife/photos/a.962151813833697/5879918952056934/
App ⼯程師都做些什麼?
•拉 UI 畫⾯ (Zeplin, Figma)
•接 API (Swagger)
•⼿機特殊功能 (例如:藍芽、相機...)
•修 Bug
接 API
•Swagger
•API Testing
Go 語⾔介紹
Golang 是 2007 年由 Google 設計的,2009 年對外公開
主要⽬的是簡化軟體開發並提⾼效率。
它在設計上強調簡單性、易讀性和⾼性能,
特別適合編寫低延遲的後端服務和分佈式系統。
Why Go ?
•強型別 (strongly typed)
•靜態型別 (statically typed)
•垃圾回收 (garbage collection)
•平⾏運算、並⾏性
•體積⼩、效能快、多平台
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
Why Go ?
https://www.docker.com/company/newsroom/media-resources/
https://kubernetes.io/
都是使⽤ Go 語⾔編寫的
GoLand by JetBrains IDE
https://www.jetbrains.com/go/
伺服器架構
•後端程式
•資料庫
•共享空間
•排程器 (Queue)
程式架構
•Data Model
•商業邏輯
•DB Interface, HTTP Interface
Go 語⾔特性
:=
短變數宣告
(Stort variable declaration)
變數建立 + 賦值 (assign) 的簡寫
package main
import "fmt"
func main() {
var a int = 3
b := 3
fmt.Println(a)
fmt.Println(b)
}
https://twitter.com/cachecoherent/status/1539867862227091456
指標(pointer)
指標 (pointer)
package main
import "fmt"
func main() {
var a int = 3
var p *int = &a
fmt.Println(a)
fmt.Println(&a)
fmt.Println(p)
fmt.Println(&p)
fmt.Println(*p)
}
// 3
// 0x14000124008
// 0x14000124008
// 0x14000126018
// 3
(印出值)
(印該變數記憶體位址)
(印出儲存的位址)
(印該變數記憶體位址)
(存取指標印出值)
Array(陣列) & Slice(切⽚)
•Array(陣列):固定⻑度 (Fixed length) 的清單
•Slice(切⽚):可以⾃由增加或減少⻑度的清單 (常⽤)
#include <stdio.h>
#include <stdlib.h>
int main() {
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(陣列)
Slice(切⽚)
https://pjchender.dev/golang/slice-and-array/
for 搭配 range
package main
import "fmt"
func main() {
var arr = []int{46, 15, 73, 16, 66, 35}
for index, value := range arr {
fmt.Printf("%d ", index, value)
}
}
Class(類別)
•Go 語⾔沒有 class (類別) 只有 struct (結構)
type Car struct {
Brand string
Year int
}
type Car struct {
Brand string
Year int
}
func (p *Car) SetBrand(brand string) {
p.Brand = brand
}
func (p *Car) SetYear(year int) {
p.Year = year
}
func (p *Car) GetBrand() string {
return p.Brand
}
func (p *Car) GetYear() int {
return p.Year
}
public class Car {
private String brand;
private int year;
public Car(String brand, int year) {
this.brand = brand;
this.year = year;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setYear(int year) {
this.year = year;
}
public String getBrand() {
return brand;
}
public int getYear() {
return year;
}
}
Golang Java
接收器
讓 struct 有擴充 method 的可能
•值接收器 (value receiver)
•指標接收器 (point receiver)
接收器
•值接收器 (value receiver)
它會複製 struct 變數並傳進函式 (method)
func (c Car) GetName() int {
return c.Name
}
•指標接收器 (point receiver)
它會把 struct 轉成指標並傳進函式 (method) [常⽤]
接收器
func (p *Car) SetYear(year int) {
p.Year = year
}
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
}
Error
package main
import (
"fmt"
"strconv"
)
func main() {
v = "s2"
s2, err := strconv.Atoi(v)
if err != nil {
fmt.Println(err) // strconv.Atoi: parsing "s2": invalid syntax
}
fmt.Printf("%T, %vn", s2, s2) // int, 0
}
Atoi (string to int)
Error
package main
import (
"fmt"
"strconv"
)
func main() {
v := "10"
s, err := strconv.Atoi(v)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%T, %vn", s, s) // int, 10
}
Atoi (string to int)
defer(推遲、延後)
(⽰意簡化版範例)
package main
import (
"fmt"
"os"
)
func main() {
userFile := "my_file.txt"
fout, err := os.Create(userFile)
if err != nil {
fmt.Println(userFile, err)
return
}
defer fout.Close()
fout.WriteString("Hello, World.")
}
channel(通道)
package main
import "fmt"
func main() {
ch := make(chan int, 1)
defer close(ch)
ch <- 1
fmt.Print(<-ch)
}
var ch chan int
ch = make(chan int)
ch := make(chan int)
宣告 塞值、取值
fatal error: all goroutines are asleep - deadlock!
緩衝區為 1
channel 型態
多執⾏緒 & WaitGroup
package main
import (
"fmt"
"sync"
)
func main() {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
// Do something
}()
go func() {
defer wg.Done()
// Do something
}()
wg.Wait()
fmt.Println("Done")
}
package main
import (
"fmt"
"sync"
)
func main() {
wg := sync.WaitGroup{}
wg.Add(2)
go worker(&wg)
go worker(&wg)
wg.Wait()
fmt.Println("Done")
}
func worker(wg *sync.WaitGroup) {
defer wg.Done()
// Do something
}
(async () => {
await Promise.all([
(async () => {
console.log('Worker 1 started');
await sleep(1000);
console.log('Worker 1 finished');
})(),
(async () => {
console.log('Worker 2 started');
await sleep(2000);
console.log('Worker 2 finished');
})(),
]);
console.log('Done');
})();
async function sleep(ms) {
return new Promise(resolve => setTimeout(() => resolve(), ms));
}
package main
import (
"fmt"
"sync"
"time"
)
func main() {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
fmt.Println("Worker 1 started")
time.Sleep(time.Millisecond * 1000)
fmt.Println("Worker 1 finished")
}()
go func() {
defer wg.Done()
fmt.Println("Worker 2 started")
time.Sleep(time.Millisecond * 2000)
fmt.Println("Worker 2 finished")
}()
wg.Wait()
fmt.Println("Done")
}
Golang JavaScript (NodeJS)
channel(通道)
package main
import "fmt"
func main() {
ch := make(chan int)
defer close(ch)
ch <- 1
fmt.Print(<-ch)
}
fatal error: all goroutines are asleep - deadlock!
⚠
無緩衝區
https://decomyplace.com/n.php?id=9357
ch := make(chan int, 1)
https://www.dnaindia.com/business/report-what-s-cooking-on-the-cloud-2752364
https://static.wixstatic.com/media/160302bd324b4187b67b7f265c3b8f24.jpg/v1/
fi
ll/w_541,h_1333,al_c,q_85,enc_auto/160302bd324b4187b67b7f265c3b8f24.jpg
?
to
ch <- 1
fatal error: all goroutines are asleep - deadlock!
⽤ range 讀取 channel
•⽤ range 讀取 channel 直到它被關閉
,若無值可取就會「等待」
•channel 關閉後,進⼊ 唯讀 狀態
•for range 會讀完 所有值 才結束
ch := make(chan int)
// ... do something
for i := range ch {
fmt.Println(i)
}
https://blog.xuite.net/yafenyan/blog/60585613#
Fan out / Fan in
https://betterprogramming.pub/writing-better-code-with-go-concurrency-patterns-9bc5f9f73519
package main
import (
"fmt"
"sync"
"time"
)
func main() {
res := distributedSum(4, 1, 100)
fmt.Printf("Total: %dn", res)
}
Fan out / Fan in 範例
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
}
func sumWorker(in chan int, 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
}
•完全⾃學!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/
參考資料
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
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
Mobile
Token (Session) JWT
Docker
Kubernetes (k8s)
Load Balancer
Database
Cache
Queue
Swagger
MongoDB
RDBMS
MySQL
PostgreSQL
Memcached
Redis
RabbitMQ
Pod
Service
Ingress
Logger
Mobile
CI / CD
grafana + prometheus
Backup
DRP (Disaster Recovery Plan)
Logs
BCP (Business Continuity Plan)
Git
Q & A
Ad

Recommended

PPTX
Docker 基礎介紹與實戰
Bo-Yi Wu
 
PPTX
Kubernetes (K8s) 簡介 | GDSC NYCU
秀吉(Hsiu-Chi) 蔡(Tsai)
 
PDF
寫給大家的 Git 教學
littlebtc
 
PPTX
工程師必備第一工具 - Git
Alan Tsai
 
PDF
Learn O11y from Grafana ecosystem.
HungWei Chiu
 
PPTX
SSL/TLS Introduction with Practical Examples Including Wireshark Captures
JaroslavChmurny
 
PDF
twMVC#44 讓我們用 k6 來進行壓測吧
twMVC
 
PDF
Microkernel Evolution
National Cheng Kung University
 
PDF
Nestjs MasterClass Slides
Nir Kaufman
 
PPTX
Managing Complex UI using xState
Xavier Lozinguez
 
PPTX
Component lifecycle hooks in Angular 2.0
Eyal Vardi
 
PDF
Defending against Java Deserialization Vulnerabilities
Luca Carettoni
 
PPTX
Unit Testing with Python
MicroPyramid .
 
PPTX
Spring boot
sdeeg
 
PDF
Spring Security
Knoldus Inc.
 
PDF
Intro to Asynchronous Javascript
Garrett Welson
 
PDF
使用 Pytest 進行單元測試 (PyCon TW 2021)
Max Lai
 
PPT
Introduction to Makefile
Tusharadri Sarkar
 
PDF
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Philip Schwarz
 
PDF
Robot framework 을 이용한 기능 테스트 자동화
Jaehoon Oh
 
PDF
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022
Johnny Sung
 
PDF
JavaScript: Variables and Functions
Jussi Pohjolainen
 
PDF
spring-api-rest.pdf
Jaouad Assabbour
 
PDF
JavaScript Promises
Derek Willian Stavis
 
PPTX
OWASP AppSecCali 2015 - Marshalling Pickles
Christopher Frohoff
 
PPTX
JavaScript Promises
L&T Technology Services Limited
 
PDF
Py.test
soasme
 
PDF
NestJS
Wilson Su
 
PDF
A Tour of Go 學習筆記
昕暐 黃
 
PPT
Go语言: 互联网时代的C
Googol Lee
 

More Related Content

What's hot (20)

PDF
Nestjs MasterClass Slides
Nir Kaufman
 
PPTX
Managing Complex UI using xState
Xavier Lozinguez
 
PPTX
Component lifecycle hooks in Angular 2.0
Eyal Vardi
 
PDF
Defending against Java Deserialization Vulnerabilities
Luca Carettoni
 
PPTX
Unit Testing with Python
MicroPyramid .
 
PPTX
Spring boot
sdeeg
 
PDF
Spring Security
Knoldus Inc.
 
PDF
Intro to Asynchronous Javascript
Garrett Welson
 
PDF
使用 Pytest 進行單元測試 (PyCon TW 2021)
Max Lai
 
PPT
Introduction to Makefile
Tusharadri Sarkar
 
PDF
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Philip Schwarz
 
PDF
Robot framework 을 이용한 기능 테스트 자동화
Jaehoon Oh
 
PDF
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022
Johnny Sung
 
PDF
JavaScript: Variables and Functions
Jussi Pohjolainen
 
PDF
spring-api-rest.pdf
Jaouad Assabbour
 
PDF
JavaScript Promises
Derek Willian Stavis
 
PPTX
OWASP AppSecCali 2015 - Marshalling Pickles
Christopher Frohoff
 
PPTX
JavaScript Promises
L&T Technology Services Limited
 
PDF
Py.test
soasme
 
PDF
NestJS
Wilson Su
 
Nestjs MasterClass Slides
Nir Kaufman
 
Managing Complex UI using xState
Xavier Lozinguez
 
Component lifecycle hooks in Angular 2.0
Eyal Vardi
 
Defending against Java Deserialization Vulnerabilities
Luca Carettoni
 
Unit Testing with Python
MicroPyramid .
 
Spring boot
sdeeg
 
Spring Security
Knoldus Inc.
 
Intro to Asynchronous Javascript
Garrett Welson
 
使用 Pytest 進行單元測試 (PyCon TW 2021)
Max Lai
 
Introduction to Makefile
Tusharadri Sarkar
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Philip Schwarz
 
Robot framework 을 이용한 기능 테스트 자동화
Jaehoon Oh
 
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022
Johnny Sung
 
JavaScript: Variables and Functions
Jussi Pohjolainen
 
spring-api-rest.pdf
Jaouad Assabbour
 
JavaScript Promises
Derek Willian Stavis
 
OWASP AppSecCali 2015 - Marshalling Pickles
Christopher Frohoff
 
JavaScript Promises
L&T Technology Services Limited
 
Py.test
soasme
 
NestJS
Wilson Su
 

Similar to [Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang) (10)

PDF
A Tour of Go 學習筆記
昕暐 黃
 
PPT
Go语言: 互联网时代的C
Googol Lee
 
PDF
模块一-Go语言特性.pdf
czzz1
 
PDF
Introduction to Golang final
Paul Chao
 
PPTX
Golangintro
理 傅
 
PPTX
Go 語言基礎簡介
Bo-Yi Wu
 
PPTX
Golang 入門初體驗
政斌 楊
 
PDF
Go
Feng Yu
 
DOC
Go Lang
Feng Yu
 
PDF
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
Justin Lin
 
A Tour of Go 學習筆記
昕暐 黃
 
Go语言: 互联网时代的C
Googol Lee
 
模块一-Go语言特性.pdf
czzz1
 
Introduction to Golang final
Paul Chao
 
Golangintro
理 傅
 
Go 語言基礎簡介
Bo-Yi Wu
 
Golang 入門初體驗
政斌 楊
 
Go Lang
Feng Yu
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
Justin Lin
 
Ad

More from Johnny Sung (20)

PDF
[GDG Build with AI] 善用現代 AI 科技:打造專屬行銷工具箱 @ GDG Changhua 彰化
Johnny Sung
 
PDF
地端自建 Kubernetes (K8s) 小宇宙 (On-premises Kubernetes) @ CNTUG 2024/11 Meetup #63
Johnny Sung
 
PDF
[AI LLM] Gemma 初體驗 @ GDG Cloud Taipei Meetup #70
Johnny Sung
 
PDF
Kubernetes 地端自建 v.s. GKE,哪個更適合你? @Devfest Taipei 2024
Johnny Sung
 
PDF
ArgoCD 的雷 碰過的人就知道 @TSMC IT Community Meetup #4
Johnny Sung
 
PDF
使用 Kong 與 GitOps 來管理您企業的 API 呼叫 @ 2024 台灣雲端大會
Johnny Sung
 
PDF
[AI / ML] 用 LLM (Large language model) 來整理您的知識庫 @Devfest Taipei 2023
Johnny Sung
 
PDF
[Flutter] Flutter Provider 看似簡單卻又不簡單的狀態管理工具 @ Devfest Kaohsiung 2023
Johnny Sung
 
PDF
與 Sign in with Apple 的愛恨情仇 @ iPlayground2020
Johnny Sung
 
PDF
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Johnny Sung
 
PDF
談談 Android constraint layout
Johnny Sung
 
PDF
炎炎夏日學 Android 課程 - Part3: Android app 實作
Johnny Sung
 
PDF
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
Johnny Sung
 
PDF
炎炎夏日學 Android 課程 - Part2: Android 元件介紹
Johnny Sung
 
PDF
炎炎夏日學 Android 課程 - Part 0: 環境搭建
Johnny Sung
 
PPTX
About Mobile Accessibility
Johnny Sung
 
PDF
Introductions of Messaging bot 做聊天機器人
Johnny Sung
 
PDF
First meet with Android Auto
Johnny Sung
 
PDF
Everything About Bluetooth (淺談藍牙 4.0) - Peripheral 篇
Johnny Sung
 
PDF
[MOPCON 2015] 談談行動裝置的 Accessibility
Johnny Sung
 
[GDG Build with AI] 善用現代 AI 科技:打造專屬行銷工具箱 @ GDG Changhua 彰化
Johnny Sung
 
地端自建 Kubernetes (K8s) 小宇宙 (On-premises Kubernetes) @ CNTUG 2024/11 Meetup #63
Johnny Sung
 
[AI LLM] Gemma 初體驗 @ GDG Cloud Taipei Meetup #70
Johnny Sung
 
Kubernetes 地端自建 v.s. GKE,哪個更適合你? @Devfest Taipei 2024
Johnny Sung
 
ArgoCD 的雷 碰過的人就知道 @TSMC IT Community Meetup #4
Johnny Sung
 
使用 Kong 與 GitOps 來管理您企業的 API 呼叫 @ 2024 台灣雲端大會
Johnny Sung
 
[AI / ML] 用 LLM (Large language model) 來整理您的知識庫 @Devfest Taipei 2023
Johnny Sung
 
[Flutter] Flutter Provider 看似簡單卻又不簡單的狀態管理工具 @ Devfest Kaohsiung 2023
Johnny Sung
 
與 Sign in with Apple 的愛恨情仇 @ iPlayground2020
Johnny Sung
 
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Johnny Sung
 
談談 Android constraint layout
Johnny Sung
 
炎炎夏日學 Android 課程 - Part3: Android app 實作
Johnny Sung
 
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
Johnny Sung
 
炎炎夏日學 Android 課程 - Part2: Android 元件介紹
Johnny Sung
 
炎炎夏日學 Android 課程 - Part 0: 環境搭建
Johnny Sung
 
About Mobile Accessibility
Johnny Sung
 
Introductions of Messaging bot 做聊天機器人
Johnny Sung
 
First meet with Android Auto
Johnny Sung
 
Everything About Bluetooth (淺談藍牙 4.0) - Peripheral 篇
Johnny Sung
 
[MOPCON 2015] 談談行動裝置的 Accessibility
Johnny Sung
 
Ad

[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)