Go 101 ตัวดำเนินการ ( Operators )ในภาษาโก

golang operators

บทความนี้จะพูดถึงเรื่องตัวดำเนินการ (Operators) และลำดับความสำคัญ (Precedence) เพื่อนำมาใช้ในการเขียนโปรแกรมในภาษาโก โดยจะแยกเป็น ตัวดำเนินการทางคณิตศาสตร์ ตรรกะ การเปรียบเทียบ และการกำหนดค่า

1.Arithmetic Operators (ตัวดำเนินการทางคณิตศาสตร์)

กำหนดให้ x = 9 , y = 5

สัญลักษณ์ตัวดำเนินการตัวอย่างผลลัพธ์
+Additionx + y14
Subtractsx – y4
*Multipliesx * y45
/Dividesx / y1
%Modulusx % y4
++Incrementx++10
Decrementy–4

ตัวอย่างโค๊ด

package main

import (
	"fmt"
)

func main() {

	x := 9
	y := 5
	
	fmt.Println("x + y = ",x + y)
	fmt.Println("x - y = ",x - y)
	fmt.Println("x * y = ",x * y)
	fmt.Println("x / y = ",x / y)
	fmt.Println("x % y = ",x % y)

	x++
	fmt.Println("x++   = ", x)
	y--
	fmt.Println("y--   = ", y)

}

ผลการรันโปรแกรม

2.Comparison Operators (ตัวดำเนินการเปรียบเทียบ)

กำหนดให้ x = 9 , y = 5

สัญลักษณ์ตัวดำเนินการตัวอย่างผลลัพธ์
==Equalx == yfalse
!=Not Equalx != ytrue
>Greater thanx > ytrue
<Less thanx < yfalse
>=Greater than or Equalx >= ytrue
<=Less than or Equalx <= yfalse

ตัวอย่างโค๊ด

package main

import (
	"fmt"
)

func main() {

	x := 9
	y := 5
	
	fmt.Println("x == y = ",x == y)
	fmt.Println("x != y = ",x != y)
	fmt.Println("x > y  = ",x > y)
	fmt.Println("x < y  = ",x < y)
	fmt.Println("x >= y = ",x >= y)
	fmt.Println("x <= y = ",x <= y)
}

ผลการรันโปรแกรม

3.Logical Operators (ตัวดำเนินการด้านตรรกะ)

กำหนดให้ x = true , y = false

สัญลักษณ์ตัวดำเนินการตัวอย่างผลลัพธ์
&&ANDx && yfalse
||ORx || ytrue
!NOT!xfalse

ตัวอย่างโค๊ด

package main

import (
	"fmt"
)

func main() {

	x := true
	y := false
	
	fmt.Println("x && y = ",x && y)
	fmt.Println("x || y = ",x || y)
	fmt.Println("!x = ", !x )
}

ผลการรันโปรแกรม

4.Bitwise Operators (ตัวดำเนินการระดับบิต)

กำหนดให้ x = 11 แปลงเป็น binary = 1011 , y = 4 แปลงเป็น binary = 1001

สัญลักษณ์ตัวดำเนินการตัวอย่างผลลัพธ์
&Binary ANDx & y1001
|Binary ORx | y1011
^Binary XORx^y10
<<Binary Left Shift x<<2101100
>>Binary Right Shifty>>210

ตัวอย่างโค๊ด

package main

import (
	"fmt"
	"strconv"
)

func main() {

	x := 11
	y := 9
	
	fmt.Println("x = ",strconv.FormatInt(int64(x), 2))
	fmt.Println("y = ",strconv.FormatInt(int64(y), 2))

	fmt.Println("x & y = ",strconv.FormatInt(int64(x & y), 2))
	fmt.Println("x | y = ",strconv.FormatInt(int64(x | y), 2))
	fmt.Println("x ^ y = ",strconv.FormatInt(int64(x ^ y), 2))
	fmt.Println("x<< = ",strconv.FormatInt(int64(x<<2), 2))
	fmt.Println("x>> = ",strconv.FormatInt(int64(x>>2), 2))
	
}

ผลการรันโปรแกรม

5.Assignment Operators (ตัวดำเนินการกำหนดค่า)

กำหนดให้ x = 5

สัญลักษณ์ตัวดำเนินการตัวอย่างผลลัพธ์
=Assignx = 55
+=Add AND Assign x += 30 เท่ากับ x = x + 3035
-=Subtract AND Assignx -= 30 เท่ากับ x = x – 305
*=Multiply AND Assignx *= 30 เท่ากับ x = x * 30150
/=Divide AND Assignx /= 30 เท่ากับ x = x / 305
%=Divide AND Assignx %= 30 เท่ากับ x = x % 305

ตัวอย่างโค๊ด

package main

import (
	"fmt"
)

func main() {

	x := 5
	
	x += 30
	fmt.Println("x = ",x)

	x -= 30
	fmt.Println("x = ",x)

	x *= 30
	fmt.Println("x = ",x)

	x /= 30
	fmt.Println("x = ",x)
	
	x %= 30
	fmt.Println("x = ",x)
	
}

ผลการรันโปรแกรม

สำหรับบทความนี้ทำให้เข้าใจ Operators สัญลักษณ์ตัวดำเนินการทางคณิตศาสตร์ ตรรกะ การเปรียบเทียบ และการกำหนดค่าพร้อมตัวอย่างพร้อมผลลัพธ์ หวังว่าคงพอจะมีประโยชน์สำหรับคนที่กำลังศึกษาบ้างไม่มากก็น้อยนะครับ.