Overview

package main
 
func main() {
 
}

Variables

// Variables
var ok bool
var s string = "hello"
 
// Type Inference
var ok = true
s := "hello"

โดยการใช้แบบ Type Inference มีข้อจำกัดคือไม่สามารถประกาศนอก body ของ function หรือในระดับ package ได้ ต้องใช้แบบ var เท่านั้น

Multiple declaration

s, ok := "hello", true

Constants

const defaultValue int = 1
const defaultTitle = "Go"
const word = "Hello"

Iota for Enums

  • iota เป็น keyword ที่ใช้ทำ Enum โดยกำหนดตัวแปรที่ประกาศให้ค่าตั้งต้นเป็น int = 0 ตัวถัดไปก็จะเพิ่มขึ้นทีละ 1
  • ถ้าประกาศตัวแปร iota + 1 ตัวแรกก็จะค่ามีตั้งต้นเป็นเริ่มที่ 1 แล้วก็นับถัดไปเหมือนเดิม
const (
	sunday = iota
	monday
	tuesday
	wednesdat
	thursday
	friday
	saturday
)

ต้องการประกาศให้ constant มี type เป็น day

type day int
const (
	sunday day = iota
	monday
	tuesday
	wednesdat
	thursday
	friday
	saturday
)

See also: Data Types and Structures

Conditions

If Statement

if name != "" && (a > 1 || b < 2) {
	fmt.Println("Hello", name)
} else {
	fmt.Println("Hello friend")
}

If with a Short Statement

Scope เฉพาะใน if เท่านั้น

limit := 225.0
v := math.Pow(10, 2)
if v < limit {
	fmt.Println("power:", v)
} else {
	fmt.Printf("power: %g over limit %g", v, limit)
}
 
// Short statement
limit := 225.0
if v := math.Pow(10, 2); v < limit {
	fmt.Println("power:", v)
} else {
	fmt.Printf("power: %g over limit %g", v, limit)
}

Switch Case

Value switch

today := "Saturday"
switch today {
case "Saturday":
	fmt.Println("Playing")
	fallthrough
case "Sunday", "Monday":
	fmt.Println("Relax")
default:
	fmt.Println("Working!!!")
}

Condition switch

num := 1
switch {
case num >= 0:
	fmt.Println("Great!!!")
case num < 0:
	fmt.Println("Sad")
default:
	fmt.Println("Confused")
}

fallthrough คือบอกว่าให้ทำ case ด้านล่างถัดไปอีก 1 case ด้วย

For Loop

Golang มีการทำวนซ้ำแบบเดียวคือ for loop ไม่มี keyword while loop การทำ for loop มี 3 รูปแบบ ดังนี้

for i := 0; i < 10; i++ {
	// A loop with 3 components
}
 
for i < 10 {
	// A loop with a condition (เทียบเท่า while)
}
 
for {
	// An infinite loop
}

Range Loop

การใช้ for loop กับสมาชิกใน Array

skills := [3]string{"JS", "Go", "Python"}
 
for i := 0; i < len(skills); i++ {
	fmt.Println(skills[i])
}
 
/* OR */
for i := range skills {
	fmt.Println(skills[i])
}

จริง ๆ แล้ว range จะคืนค่าออกมาได้ 2 ค่า คือ index, value

for i, val := range skills {
	fmt.Println("index:", i, "value:", val)
}

เราสามารถละค่าตัวแปรแรกเพื่อใช้แต่ตัวที่สองได้ โดยใช้ _

for _, val := range skills {
	fmt.Println("value:", val)
}

See also: Go Data Structures for more on ranges with different types

Type Conversions

การเปลี่ยนชนิดข้อมูลให้ตัวแปร

var i int = 1
fmt.Printf("type: %T, value: %v\n", i, i)
 
var f float64 = float64(i) // type conversion
fmt.Printf("type: %T, value: %v\n", f, f)

String Conversion

ในกรณีเปลี่ยนจาก string เป็นข้อมูลชนิดตัวเลข ต้องใช้ Package strconv

v := "42"
s, err := strconv.Atoi(v)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(s)
 
// convert int to string
i := 10
n := strconv.Itoa(i)
fmt.Printf("%T, %v\n", n, n)