
ภาษา Go เป็นภาษาใหม่ที่ทาง Google ได้พัฒนาขึ้นมาในรูปแบบของ OpenSource Project โดยทีมนักพัฒนาของ Google สำหรับแนวทางเลือกใหม่การเขียนโปรแกรม
อันที่จริงโครงการนี้เป็นโครงการที่ Google ได้วางนโยบายให้ พนักงานใช้เวลาว่าง 20% ของเวลางานไปทำอะไรก็ได้ที่อยากจะทำแบบ Capstone (แนวทางการศึกษาด้วยตนเอง) ก็เลยได้เกิดเจ้าโครงการภาษา Go นี้มา แต่สำหรับส่วนตัวของผม ผมแค่รู้สึกว่าเจ้า Go เฟอร์ สัญลักษณ์ของภาษานี้มันน่ารักดี ประกอบกับมีพี่น้องนักพัฒนาชาวไทยของเราเริ่มหันมาใช้ภาษา Go มาพัฒนา API และ Web Services ด้วยคุณสมบัติของความรวดเร็วของมัน
เริ่มต้นก็สามารถเข้าไปติดตั้ง และดูวิธีการ Set PATH ได้เลยครับที่เว็บไซต์: https://golang.org/dl/

สำหรับผมใช้ Mac OSX ไม่มีปัญหา อะไรแต่ถ้าเป็น windows ก็ตั้งค่ากันหน่อยละกัน ตามข้อมูลข้างล่าง
| OS | Output |
|---|---|
| Linux | export PATH=$PATH:/usr/local/go/bin |
| Mac | export PATH=$PATH:/usr/local/go/bin |
| FreeBSD | export PATH=$PATH:/usr/local/go/bin |
สำหรับ Mac ก็ติดตั้งไฟล์ DMG ซะ จะเจอสัญลักษณ์เจ้า Go เฟอร์น่ารักปรากฏขึ้นมา

เมื่อเสร็จแล้วก็เปิด Editor สักตัวขึ้นมาลองเริ่มต้นเขียนโปรแกรมครับ ซึ่งโครงสร้างของภาษา Go นั้นใกล้เคียง C, Python และ Swift ผสมกันเลย สำหรับคำสงวนของภาษา Go ก็ประกอบไปด้วย:
| break | default | func | interface | select |
| case | defer | go | map | struct |
| chan | else | goto | package | switch |
| const | fallthrough | if | range | type |
| continue | for | import | return | var |
เปิด Text Editor ขึ้นมาแล้วลองเริ่มเขียนคำสั่ง Basic ของมันดูครับ:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
ทำการบันทึกไฟล์เป็น tutorial1.go ซะ แล้วไป compile สำหรับ run มันผ่าน Terminal (ถ้าเป็น Windows ก็คือ Command Line) ครับผลลัพธ์ก็จะเป็นดังนี้

ทดสอบเรื่องของตัวแปรและ DataType ของภาษา Go หน่อย สร้างไฟล์ว่า tutorial2.go ขึ้นมา:
package main
import "fmt"
func main() {
var x float64 = 20.0
y := 42
fmt.Println(x)
fmt.Println(y)
fmt.Printf("x is of type %T\n", x)
fmt.Printf("y is of type %T\n", y)
}
ผลลัพธ์ก็คือ:

ทดสอบนิพจน์ทาง คณิตศาสตร์หน่อย สร้างไฟล์ tutorial3.go
package main
import "fmt"
func main() {
var a int = 21
var c int
c = a
fmt.Printf("Result 1 - = Operator Example, Value of c = %d\n", c )
c += a
fmt.Printf("Result 2 - += Operator Example, Value of c = %d\n", c )
c -= a
fmt.Printf("Result 3 - -= Operator Example, Value of c = %d\n", c )
c *= a
fmt.Printf("Result 4 - *= Operator Example, Value of c = %d\n", c )
c /= a
fmt.Printf("Result 5 - /= Operator Example, Value of c = %d\n", c )
c = 200;
c <<= 2
fmt.Printf("Result 6 - <<= Operator Example, Value of c = %d\n", c )
c >>= 2
fmt.Printf("Result 7 - >>= Operator Example, Value of c = %d\n", c )
c &= 2
fmt.Printf("Result 8 - &= Operator Example, Value of c = %d\n", c )
c ^= 2
fmt.Printf("Result 9 - ^= Operator Example, Value of c = %d\n", c )
c |= 2
fmt.Printf("Result 10 - |= Operator Example, Value of c = %d\n", c )
}
ผลลัพธ์

เป็นการชิมลางเล็กน้อยกับ เจ้าภาษา Go เท่านี้ก่อนละกันนะครับในบทเรียนนี้




2 Comments