จากบทความ 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 (ชนิดข้อมูลตัวเลขจำนวนเต็ม)
Type | Size | Values |
int | Signed 32 bit or 64 bit | Platform dependent ขึ้นอยู่กับเครื่องที่รันเป็น 32 bit หรือ 64 bit |
uint | Unsigned 32 bit or 64 bit | Platform dependent ขึ้นอยู่กับเครื่องที่รันเป็น 32 bit หรือ 64 bit |
int8 | Signed 8 bit | -128 to 127 |
int16 | Signed 16 bit | -32768 to 32767 |
int32 | Signed 32 bit | -2147483648 to 2147483647 |
int64 | Signed 64 bit | -9223372036854775808 to 9223372036854775807 |
uint8 | Unsigned 8-bit integers | 0 to 255 |
uint16 | Unsigned 16-bit integers | 0 to 65535 |
uint32 | Unsigned 32-bit integers | 0 to 4294967295 |
uint64 | Unsigned 64-bit integers | 0 to 18446744073709551615 |
2.Floating Type (ชนิดข้อมูลจำนวนจริง(ทศนิยม))
Type | Reference | Values |
float32 | IEEE-754 32-bit floating-point numbers | 1.2E-38 to 3.4E+38 |
float64 | IEEE-754 64-bit floating-point numbers | 1.2E-38 to 3.4E+38 (เก็บข้อมูลละเอียดกว่า float32) |
complex64 | จำนวนเชิงซ้อน ส่วนจริงและส่วนจินตภาพ | ส่วนจริง (float32) และส่วนจินตภาพ (float32) |
complex128 | จำนวนเชิงซ้อน ส่วนจริงและส่วนจินตภาพ | ส่วนจริง (float64) และส่วนจินตภาพ (float64) |
3.Boolean Type (ชนิดข้อมูลแบบตรรกะ)
Type | Size | Values |
bool | 1 bytes | true or false |
4.String Type (ชนิดข้อมูลตัวอักษรข้อความ)
Type | Size | Values |
string | UTF-8-encoded text (1 – 4 Bytes) | “ABCDEFG” |
5.Other Type (ชนิดข้อมูลอื่นๆ)
Type | Size | Values |
byte | uint8 | ‘a’, ‘c’, ‘d’ |
rune | int32 | ‘♥’, ‘£’ |
uintptr | Unsigned 32 bit or 64 bit | Platform 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
ผลลัพธ์
การเลือกชนิดของข้อมูลก็เป็นส่วนที่สำคัญที่เราต้องใช้งานให้ตรงกับวัตถุประสงค์ เพื่อให้ได้ความถูกต้องและมีประสิทธิภาพของโปรแกรมในการใช้งานทรัพยากรระบบ
หวังว่าจะเป็นประโยชน์กับคนที่สนใจนะครับ.