Go 101 ชนิดข้อมูลในภาษาโก (Data Type)

golang-datatype

จากบทความ Go Variables, Go Identfiers & Keywords ว่าด้วยเรื่องตัวแปรในภาษาโก หลักการประกาศ การกำหนดค่าข้อมูล มาถึงบทความนี้เราก็จะมาเรียนรู้ว่าชนิดของข้อมูลตัวแปร ในภาษาโกมีอะไรกันบ้าง

  • Integer type คือชนิดข้อมูลตัวเลขจำนวนเต็ม เช่น 1, 10, 9889, 66567432
  • Floating type คือชนิดข้อมูลจำนวนจริง(ทศนิยม) เช่น 1.0, 55.82, 4566.899, 345434.3344
  • Boolean type คือชนิดข้อมูลแบบตรรกะ คือ true ค่าจริง และ false ค่าเท็จ
  • String type คือชนิดข้อมูลตัวอักษรข้อความ เช่น “dekdoydev.com”, “go lang 101”
  • Pointer type (ขออธิบายแยกสำหรับตัวแปร Pointer โดยเฉพาะ)

ส่ิงที่เราต้องคำนึงถึงข้อมูลแต่ละชนิดในการนำมาเขียนโปรแกรมคือ ประเภทข้อมูล ขนาดในการเก็บข้อมูล และค่าที่เก็บได้ดังตาราง

1.Integer Type (ชนิดข้อมูลตัวเลขจำนวนเต็ม)

TypeSizeValues
intSigned 32 bit or 64 bitPlatform dependent ขึ้นอยู่กับเครื่องที่รันเป็น 32 bit หรือ 64 bit
uintUnsigned 32 bit or 64 bitPlatform dependent ขึ้นอยู่กับเครื่องที่รันเป็น 32 bit หรือ 64 bit
int8Signed 8 bit-128 to 127
int16Signed 16 bit-32768 to 32767
int32Signed 32 bit-2147483648 to 2147483647
int64Signed 64 bit-9223372036854775808 to 9223372036854775807
uint8Unsigned 8-bit integers0 to 255
uint16Unsigned 16-bit integers0 to 65535
uint32Unsigned 32-bit integers0 to 4294967295
uint64Unsigned 64-bit integers0 to 18446744073709551615

2.Floating Type (ชนิดข้อมูลจำนวนจริง(ทศนิยม))

TypeReferenceValues
float32IEEE-754 32-bit floating-point numbers1.2E-38 to 3.4E+38
float64IEEE-754 64-bit floating-point numbers1.2E-38 to 3.4E+38 (เก็บข้อมูลละเอียดกว่า float32)
complex64จำนวนเชิงซ้อน ส่วนจริงและส่วนจินตภาพส่วนจริง (float32) และส่วนจินตภาพ (float32)
complex128จำนวนเชิงซ้อน ส่วนจริงและส่วนจินตภาพส่วนจริง (float64) และส่วนจินตภาพ (float64)

3.Boolean Type (ชนิดข้อมูลแบบตรรกะ)

TypeSizeValues
bool1 bytestrue or false

4.String Type (ชนิดข้อมูลตัวอักษรข้อความ)

TypeSizeValues
stringUTF-8-encoded text (1 – 4 Bytes)“ABCDEFG”

5.Other Type (ชนิดข้อมูลอื่นๆ)

TypeSizeValues
byteuint8‘a’, ‘c’, ‘d’
runeint32‘♥’, ‘£’
uintptrUnsigned 32 bit or 64 bitPlatform dependent ขึ้นอยู่กับเครื่องที่รันเป็น 32 bit หรือ 64 bit เพื่อเก็บข้อมูล Pointer address

ตัวอย่าง

package main

import (
    "fmt"
    "math/bits"
    "reflect"
    "unsafe"
)

func main() {
	
	rPound := '£'
	
	fmt.Printf("Type: %s\n", reflect.TypeOf(rPound))
	fmt.Printf("Unicode CodePoint: %U\n", rPound)
	fmt.Printf("Character: %c\n", rPound)

    sizeOfIntInBits := bits.UintSize
    fmt.Printf("%d bits\n", sizeOfIntInBits)
    
    var a int
    fmt.Printf("%d bytes\n", unsafe.Sizeof(a))
    fmt.Printf("a's type is %s\n", reflect.TypeOf(a))
    
    b := 2
    fmt.Printf("b's typs is %s\n", reflect.TypeOf(b))
}
  • บรรทัด 12 ประกาศตัวแปร rune เก็บสัญลักษณ์ ‘£’
  • บรรทัด 14 แสดงชนิดของตัวแปร rPound เป็นชนิด int32
  • บรรทัด 15 แสดงรหัส unicode ของ ‘£’ มีค่าเท่ากับ U+00A3
  • บรรทัด 18,19 แสดงขนาดของ uint มีค่าเท่ากับ 64 bit
  • บรรทัด 21,22,23 แสดงขนาดและชนิดตัวแปร int คือ 8 bytes และเป็นชนิด int
  • บรรทัด 25,26 แสดงให้เห็นว่าถ้าประกาศตัวแปรที่ไม่ได้ระบุชนิด ค่า default จะเป็นชนิด int

ผลลัพธ์

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

หวังว่าจะเป็นประโยชน์กับคนที่สนใจนะครับ.