หน้าแรกผลงานความสำเร็จปรึกษาโปรเจกต์ของคุณ
ปรึกษาโปรเจกต์ของคุณ
Go lang starter กับ 7 สิ่งที่ควรรู้ก่อนเริ่ม
เทคโนโลยี

Go lang starter กับ 7 สิ่งที่ควรรู้ก่อนเริ่ม

15 ต.ค. 2569

Golang Starter กับ 7 สิ่งที่ควรรู้ก่อนเริ่ม

บทความมีเนื้อหาแบบ BEGINNER สำหรับผู้ที่ต้องการเริ่มเขียน Go lang

Article illustration 1
Article content divider

ก่อนจะเริ่มเนื้อหาก่อนอื่นขอแนะนำตัว Go lang กันก่อนนะครับว่ามันคืออะไร “ต่อไปจะเหลือแค่คำว่า Go นะครับ”

Go เป็นภาษาทางโปรแกรมมิ่งภาษาหนึ่งที่ถูกพัฒนาขึ้น และเป็น opensource ที่ใครๆ ก็สามารถนำไปใช้งานได้ จุดเด่นหลักๆ ของตัว Go นั้นลักษณะ code จะมีลักษณะการเขียนที่ทำให้เหมือนกับภาษาพูด มีความสั้นกระทัดรัด clean code และมีการกำหนดค่า type ของตัวแปรด้วย “ในที่นี้ Doc เค้าบอกว่าตัว go เนี่ย compiled เร็วด้วยนะ”

เมื่อรู้จัก Go กันแล้วเรามารู้จัก 7 สิ่งที่ควรรู้ก่อนเริ่มเขียน Go lang กันเลย

1.Getting Started With Go “เริ่มต้นลง Go เข้าเครื่องกันก่อนเลยจร้า”

https://golang.org/ ก่อนอื่นทำการติดตั้งตัว Go ลงเครื่องก่อนเลยเพื่อที่เราจะได้ทดสอบตัว sourcecode กันได้ครับ แต่สำหรับใครที่ไม่อยากลงในเครื่องเราสามารถทดสอบตัว code แบบออนไลน์ได้นะ ผ่านตัวนี้เลยครับ golang playground

2.Go Basic Types

อย่างที่เรารู้ๆ กันการกำหนด type ของตัวแปรมีผลต่อการ code อย่างมากเรามารู้จักการประกาศ type ของ Go กัน

var myint int

var mystring string

var isTrue bool = true

อันนี้เป็นตัวอย่างการประกาศตัวแปรและกำหนด type แบบแรกนะครับ อีกแบบนั้นจะเป็นแบบนี้

myfloat := 1.0000

mystring := 'string'

หมายความว่าตัวแปรจะมี type เดียวกับค่าของตัวแปร

3.Composite Types

ตัว obj และ array ต่างๆ ของ Go จะต้องมีการประกาศ type ด้วยเสมอเช่นกัน ยกตัวอย่าง

ประกาศค่า array

// declaring an empty array of strings

var days []string

 

// declaring an array with elements

days := [...]string{"mon", "tue", "wed", "thu", "fri", "sat", "sun"}

ประกาศค่า obj

// our Person struct

type Person struct {

  name string

  age int

}

 

// declaring a new `Person`

var myPerson Person

// declaring a new `elliot`

elliot := Person{name: "Elliot", age: 24}

 

// trying to roll back time to before I was injury prone

elliot.age = 18

4.Go Functions

หลังจากที่เรารู้จักการใช้งานของตัวแปรแล้วเรามารู้จักการใช้งาน Functions กัน

func myfunction(firstName string, lastName string) (string, error) {

  return firstName + " " + lastName, nil

}

จากตัว code

  • func เป็นการบอกว่าส่วนนี้เป็น function

  • myfunction(params1 type, params2 type) คือชื่อของ function และตัวแปรที่รับมา

  • (resualt_type1, resualt_type2) คือ type ของค่า return function

5.Go Methods

สำหรับส่วนนี้มีความใกล้เคียงกับการเขียนด้วยแนวคิด OOP คือ การทำให้ function เข้าไปอยู่ในส่วนของ type หรือ class เพื่อง่ายต่อการเรียกใช้ code สะอาดตาดูไม่เลอะเทอะ อ่านง่าย

package main

 

import (

    "fmt"

)

 

type Employee struct {

    Name string

}

 

func (e *Employee) UpdateName(newName string) {

    e.Name = newName

}

 

func (e *Employee) PrintName() {

    fmt.Println(e.Name)

}

 

func main() {

    var employee Employee

    employee.Name = "Elliot"

    employee.UpdateName("Forbsey")

    employee.PrintName()

}

จากตัวอย่างจะทำให้ตัว Employee type มี function UpdateName() และ PrintName() ด้วย

6.Go Interfaces

เป็นการประกาศ type ที่ตัว type มีการ return เป็น function บางอย่าง

package main

 

import "fmt"

 

type Guitarist interface {

    PlayGuitar()

}

 

type BaseGuitarist struct {

    Name string

}

 

type AcousticGuitarist struct {

    Name string

}

 

func (b BaseGuitarist) PlayGuitar() {

    fmt.Printf("%s plays the Bass Guitar\n", b.Name)

}

 

func (b AcousticGuitarist) PlayGuitar() {

    fmt.Printf("%s plays the Acoustic Guitar\n", b.Name)

}

 

func main() {

    var player BaseGuitarist

    player.Name = "Paul"

    player.PlayGuitar()

 

    var player2 AcousticGuitarist

    player2.Name = "Ringo"

    player2.PlayGuitar()

}

7.Go Project Structure

  • การประการชื่อของ function ให้ใช้การประกาศชื่อแบบ Pascal case

  • การประกาศชื่อของตัวแปรใช้การประกาศชื่อแบบ Camel case

  • การประกาศชื่อของ package ใช้การประกาศชื่อแบบ Snake case

อ้างอิง

  • https://golang.org/doc/

  • https://tutorialedge.net/course/golang/

บทความที่เกี่ยวข้อง

เทคโนโลยี

TypeScript คืออะไร? แล้วคุณรู้หรือไม่ว่าใช้งานยังไง

บทความแนะนำภาษา TypeScript ว่าคืออะไร ทำไมถึงควรเรียนรู้ และช่วยให้การเขียนโปรแกรมปลอดภัยและจัดการง่ายขึ้นอย่างไร

เทคโนโลยี

มาทำความรู้จักกับ Lodash กัน

ก่อนอื่นเลยต้องขอสวัสดีพี่ๆน้องเพื่อนๆ ชาวโปรแกรมเมอร์ กันก่อนนะครับ สำหรับ Blog นี้ผมจะพาทุกๆคนไปทำความรู้จักกับ Libary…

เทคโนโลยี

Multiple Language ด้วย i18n บน nuxt.js Framework

สวัสดีครับ พอดีผมได้มีโอกาศลองใช้งานเจ้าตัว i18n เห็นว่าเจ้าตัวเนี่ย มันใช้งานค่อนข้างง่ายพอสมควรครับ วันนี้เลยจะมาแชร์ถึงการ setting…

I GEAR GEEK

Technology Partner ขับเคลื่อน Digital Transformation ให้ธุรกิจคุณ

โซลูชัน

  • ERP และระบบธุรกิจ
  • AI Knowledge และ AI Agents
  • Customer Data & Marketing Automations

บริการ

  • Web Application, Mobile Application

บริษัท

  • บทความ
  • แกลเลอรี
  • ร่วมงานกับเรา

ติดต่อ

marketing@igeargeek.com

Malada space ชั้น 2, 89/9 หมู่ 5 ตำบลสุเทพ อำเภอเมืองเชียงใหม่ จังหวัดเชียงใหม่ 50200

LINE OA@igeargeekสอบถามโปรเจกต์083-579-5068สมัครงานและฝึกงาน083-254-2241
© 2026 I GEAR GEEK สงวนลิขสิทธิ์
ปรึกษาโปรเจกต์ของคุณ