คุกกี้ที่คุณเลือกได้

เราใช้คุกกี้ที่จำเป็นเพื่อให้เว็บไซต์ทำงาน ส่วนการวิเคราะห์และการตลาดจะเริ่มเมื่อคุณอนุญาตเท่านั้น คุณเปลี่ยนใจได้ที่ “ตั้งค่าคุกกี้” ด้านล่างของเว็บไซต์ นโยบายความเป็นส่วนตัว

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

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

3 ธ.ค. 2563

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/

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

รีวิวการสอบ AWS Solution Architect —  Associate Certification (SAA-C03) ปี 2024 (Online) แบบสั้น

เทคโนโลยี

รีวิวการสอบ AWS Solution Architect —  Associate Certification (SAA-C03) ปี 2024 (Online) แบบสั้น

รีวิวการสอบ AWS Solution Architect — Associate Certification(SAA-C03) ปี 2024 (Online) แบบสั้น Solution Architect — Associate Certificate เป็นใบรับรองความร

Setup Infrastructure ง่าย ๆ ด้วย Terraform

เทคโนโลยี

Setup Infrastructure ง่าย ๆ ด้วย Terraform

แนะนำ Terraform และแนวคิด Infrastructure as Code พร้อมตัวอย่าง setup AWS แบบเข้าใจง่าย สำหรับเริ่มจัดการ infrastructure ให้ทำซ้ำได้

การใช้งาน MySQLใน Go Fiber

เทคโนโลยี

การใช้งาน MySQLใน Go Fiber

สวัสดีค่ะ ในบทความนี้เราจะมาพูดถึงวิธีการใช้งานฐานข้อมูลใน Go Fiber กันค่ะ การเชื่อมต่อและจัดการข้อมูลในฐานข้อมูลเป็นส่วนสำคัญของการพัฒนาแอปพลิเคชัน ในที่น

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 สงวนลิขสิทธิ์
ปรึกษาโปรเจกต์ของคุณ