สรุปความรู้จากการฟัง Live : Golang Workshop [Part I]

thip
thip
Aug 26, 2017 · 4 min read

เริ่มด้วยการติดตั้ง (Windows)

  1. download → ตัว install ที่ https://golang.org/dl/ ล่าสุดเป็น version 1.8.3
  2. install → ติดตั้งจากไฟล์ go1.8.3.windows-amd64.msi ที่ download มาแล้ว
  3. test → ทดสอบการติดตั้งด้วยการเข้า command prompt แล้วพิมพ์คำสั่ง go version ผลลัพธ์จะแสดงข้อมูล version ของ Go ที่เราติดตั้งไป

Agenda

  • Basic of TDD
  • Basic of Go
  • Using interface
  • Building REST APIs
  • Testing REST APIs
  • Working with MongoDB

1. Basic of TDD

Test Driven Development life cycle คือ

write a failing test → Make the test pass → Refactor

เขียนโค้ดให้เฟล เขียนเทสให้ผ่าน แล้ว refactor

แต่ก่อนอื่นเราต้อง “คิด” ก่อนว่า เราจะเทสอะไร? ก็ย้อนกลับไปที่ เรากำลังจะสร้างอะไร? เพื่อแก้ปัญหาอะไร?

กรณีที่แยกการพัฒนาออกเป็น Front-end, Back-end (APIs/Microservices) ควรทำการทดสอบในแบบมุมมองของ End-to-End Testing ให้เขียน Test จนรู้สึกว่า เรามั่นใจและสบายใจในโค้ดที่เราเขียนแล้ว

:: Test Level

GUI Tests — API Layer Acceptance Tests — Unit Tests/Component Tests

:: TDD Rules

ต้องเขียน test ที่ fail ก่อนเขียนโค้ดทุกครั้ง แล้วค่อย remove dupplicate

:: ข้อเสียของการทำ TDD

  • เสียเวลาเพิ่ม เพราะต้องเขียนโค้ดเพิ่ม
  • มีโค้ดที่ต้องดูแลเพิ่มมากขึ้น
  • ระบบใหญ่ถ้าจะทำ TDD อาจกระทบไปถึงต้องเปลี่ยน Design

“ถ้ากำลังคิดว่าจะเขียน Test ดีไหม? ให้เอาเวลาตรงนั้นมาลงมือเขียนเลย” การทำ TDD มันมีข้อดีในแง่ของ long term

2. Basic of Go

:: ทำไมต้องใช้ Go

  • เร็ว (ในแง่ dev/deploy)
  • เขียนง่าย
  • scale ได้
  • ติดตั้งง่าย install msi จบ
  • no dependencies
  • no exceptions มันจะ return เป็น error เลย แล้วคนเรียกต้องไปดักเช็คเอง
  • small core language

:: Resource Developer

Web: Go Tour

Book: Effective go

Github: Learn go

Hello Go

:: ส่วนประกอบ

package

import ← ทำได้แบบทีละ import หรือจะ import หลายสิ่งใน line เดียวก็ได้

func main()

:: Note

  • ไฟล์นามสกุล .go /ตัวเล็กหมด/ชื่อสั้นๆ
  • ไม่มี access modifier (public ใช้ขึ้นต้นตัวพิมพ์ใหญ่, private ใช้ขึ้นต้นตัวพิมพ์เล็ก)
  • initial variable แบบไม่ต้องระบุ type
  • semi colon is optional
  • := เป็นการ initial และ assign ในเวลาเดียวกัน
  • import แล้วไม่ใช้งาน จะ compile ไม่ผ่าน
  • ไม่มี null มีแต่ nil
  • การตั้งชื่อตัวแปร จะใช้แบบ camel case
  • มีการจัด format ของโค้ด ผ่าน command go fmt xxx.go
  • ภาษา Go ไม่มี class แต่เป็น OOP

:: การ Run

go run xxx.go

:: การ build

go build xxx.go ← ได้ไฟล์ xxx.exe รันผ่าน command xxx.exe ได้เลย

จะได้ binary file 1 ไฟล์ ที่พร้อมเอาไป deploy แบบที่ไม่ต้องการสิ่งใดอีก มันจะ pack สิ่งที่จำเป็นต้องใช้ใส่ไว้ในไฟล์เดียวเลย

:: Note

  • ถ้าจะ build ข้าม OS เราสามารถ config Go environments ได้ เช็คผ่าน command go env แล้วสามารถ export ตัวแปรแก้ไขค่า OS ที่ต้องการได้เลย Wow!
  • godoc เป็นคำสั่งใช้ดู document ของคำสั่ง Go ต่างๆ ตัวอย่าง godoc fmt Printf

Types

  • Number (int,float,complex)
  • String
  • Boolean (true,false)

Variable

  1. var a string = “hello”
  2. var b string

b = “hello”

3. var c = “hello”

4. d := “hello”

Zero Value

= ค่าเริ่มต้นของตัวแปร

  • int → 0
  • float → 0
  • boolean → false
  • string → “”

Constant

= ค่าคงที่ ตัวอย่าง → const name string = “Noodee”

Multiple Variables

  • ประกาศตัวแปลรวบเป็นชุดก็ได้
  • ประกาศหลายตัวแปรรวมกันก็ได้
  • import เป็นชุดเดียวเลยก็ได้
  • แยก import เป็นรายบรรทัดก็ได้

Control flow statement

  • For → loop มีแต่ for อย่างเดียว ไม่มี ()ด้วย
  • If → แบบไม่ต้องใส่วงเล็บ
  • Switch → แบบไม่ต้องใส่วงเล็บ, ไม่มี break

Data structure

  • Array มี blank identifier คือ _ หมายถึงจำเป็นต้องประกาศสองตัวแปร แต่ไม่ได้ใช้งาน จึงต้องประกาศแบบ _ ไม่งั้นจะ compile ไม่ผ่าน

initial ค่าแบบหลายบรรทัดได้ เพื่อสะดวกในการ comment แต่ค่าสุดท้ายต้องปิดด้วย comma! → ส่วนใหญ่ใช้ตอนทำ data test

  • Slice → แก้ไข size ได้!! ขั้นกว่าของ array, ใช้เยอะ, เข้าถึงข้อมูลได้ง่าย, performance ดีกว่า array / สร้างผ่าน make function / เรื่องของ size จะมี len กับ cap (capacity) / การใส่ค่าและการดึงค่าเหมือนกับ Array เลย

:: Note

Array & Slice มี 2 build-in function คือ append (แล้วต้องเอาตัวแปรใหม่มารับค่า) และ copy

  • Map สร้างผ่าน make & map function / การดึงค่าจาก map จะ return value และ boolean (true=มีข้อมูล, false=ไม่มีข้อมูล)

thipwriteblog

Programming, Technology, WorkLife, Storyteller, Lifestyle

)

thip

Written by

thip

ฝากติดตามเรื่องเล่าสั้นๆที่ Facebook Page : thip อีกหนึ่งช่องทางด้วยนะคะ : )

thipwriteblog

Programming, Technology, WorkLife, Storyteller, Lifestyle

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade