Skip to main content

Type และ Struct ใน Go

การประกาศ Type ใน Go

go สามารถประกาศชนิดของตัวแปรขึ้นมาเองได้ โดยการใช้ keyword "type" นำหน้า

type <ชื่อ type> ชนิดของตัวแปร

Example:

type fahrenheit float32
type celcius float32

จากโค้ดตัวอย่างข้างบน ชนิดตัวแปร fahrenheit และ celcius มีชนิดเป็น float32 ทั้งคู่แต่ไม่สามารถนำมาใช้แทนกันได้ เราประกาศ type ขึ้นมาเองเพื่อความง่ายในการเข้าใจโค้ด

Example: เกิด error ขึ้นเมื่อพยายามใช้ type ที่ต่างกัน

package main

type fahrenheit float32
type celcius float32

func main() {
  var tem1 fahrenheit = 235.23
  var tem2 celcius = tem1 // error
}



การประกาศ Struct ใน Go

struct ใน ภาษา Go คือ ตัวแปรแบบ user-defined ก็คือผู้เขียนเป็นคนกำหนดเอง ซึ่ง struct สามารถรวมตัวแปรหลายๆตัว และหลายประเภทเข้าเข้ามาอยู่รวมกันเป็น type เดียวกันได้ concept ของ struct จะคล้ายกับ class ในภาษาที่เป็น OOP

type <ชื่อ struct> struct {
  // ข้อมูลเกี่ยวกับ struct นั้นๆ
}

ก่ารนำ struct ไปใช้งาน

var <ชื่อตัวแปร> <ชื่อ struct> = <ชื่อ struct>{/*ตามด้วยข้อมูลที่เราต้องใส่ใน struct*/}

// หรือ ใช้ shorthand declaration (type inference declaration)

<ชื่อตัวแปร> := <ชื่อ struct>{/*ตามด้วยข้อมูลที่เราต้องใส่ใน struct*/}


การประกาศและนำ struct ไปใช้ มีให้ดังตัวอย่างต่อไปนี้

Example 1: การประกาศ struct และ การนำ struct ไปใช้

package main

import "fmt"

// struct declaration
type User struct {
  name string
  age uint
  email string
}

func main() {
  var john User = User{
    name: "John",
    age: 20,
    email: "[email protected]",
  }
  // หรือ
  var john User = User{"John", 20, "[email protected]", }
  
  fmt.Println(john) // {John 20 [email protected]}
}


ตัวอย่างต่อไปนี้แสดงการดึงข้อมูลจาก struct ที่เราสร้างขึ้นมาแล้วมาใช้ และการ declare struct ซึงมี function อยู่ด้วย

Example 2: การประกาศ struct ซึ่งมี function รวมอยู่ด้วย

package main

import "fmt"

// struct declaration
type User struct {
  name  string
  age   uint
  email string

  // struct สามารถมี function รวมอยู่ด้วย
  speak func(string) string
}

func main() {
  var john User = User{
    name:  "John",
    age:   20,
    email: "[email protected]",
    speak: func(str string) string {
      return "User say: " + str
    },
  }
  fmt.Println("Name: ", john.name)		// Name:  John
  fmt.Println("Age: ", john.age)		// Age:  20
  fmt.Println("Email: ", john.email)	// Email:  [email protected]
  fmt.Println(john.speak("Hi"))			// User say: Hi
}

จากตัวอย่างด้านบนจะเห็นว่าเราสามารถเข้าถึงข้อมูลที่เก็บไว้ใน struct โดยใช้ ชื่อของ (john) ตัวแปรที่ประกาศชนิดเป็น User


Example 3: การใช้ struct ในการเก็บข้อมูลคู่อันดับในกราฟ

package main

import (
  "fmt"
  "math"
)

type Point struct {
  x float64
  y float64
}

func main() {
  var p1 Point = Point{12.3, 20.0}
  var p2 Point = Point{5.4, 15.2}
  fmt.Println("Distance between p1 and p2 is ", findDistance(p1, p2)) // Distance between p1 and p2 is  8.40535543567314
}

func findDistance(p1 Point, p2 Point) float64 {
  var deltaX float64 = math.Abs(p1.x - p2.x)
  var deltaY float64 = math.Abs(p1.y - p2.y)
  return math.Sqrt(deltaX*deltaX + deltaY*deltaY)
}