บทความนี้จะพูดถึงเรื่องตัวดำเนินการ (Operators) และลำดับความสำคัญ (Precedence) เพื่อนำมาใช้ในการเขียนโปรแกรมในภาษาโก โดยจะแยกเป็น ตัวดำเนินการทางคณิตศาสตร์ ตรรกะ การเปรียบเทียบ และการกำหนดค่า
1.Arithmetic Operators (ตัวดำเนินการทางคณิตศาสตร์)
กำหนดให้ x = 9 , y = 5
สัญลักษณ์ | ตัวดำเนินการ | ตัวอย่าง | ผลลัพธ์ |
+ | Addition | x + y | 14 |
– | Subtracts | x – y | 4 |
* | Multiplies | x * y | 45 |
/ | Divides | x / y | 1 |
% | Modulus | x % y | 4 |
++ | Increment | x++ | 10 |
— | Decrement | y– | 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
สัญลักษณ์ | ตัวดำเนินการ | ตัวอย่าง | ผลลัพธ์ |
== | Equal | x == y | false |
!= | Not Equal | x != y | true |
> | Greater than | x > y | true |
< | Less than | x < y | false |
>= | Greater than or Equal | x >= y | true |
<= | Less than or Equal | x <= y | false |
ตัวอย่างโค๊ด
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
สัญลักษณ์ | ตัวดำเนินการ | ตัวอย่าง | ผลลัพธ์ |
&& | AND | x && y | false |
|| | OR | x || y | true |
! | NOT | !x | false |
ตัวอย่างโค๊ด
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 AND | x & y | 1001 |
| | Binary OR | x | y | 1011 |
^ | Binary XOR | x^y | 10 |
<< | Binary Left Shift | x<<2 | 101100 |
>> | Binary Right Shift | y>>2 | 10 |
ตัวอย่างโค๊ด
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
สัญลักษณ์ | ตัวดำเนินการ | ตัวอย่าง | ผลลัพธ์ |
= | Assign | x = 5 | 5 |
+= | Add AND Assign | x += 30 เท่ากับ x = x + 30 | 35 |
-= | Subtract AND Assign | x -= 30 เท่ากับ x = x – 30 | 5 |
*= | Multiply AND Assign | x *= 30 เท่ากับ x = x * 30 | 150 |
/= | Divide AND Assign | x /= 30 เท่ากับ x = x / 30 | 5 |
%= | Divide AND Assign | x %= 30 เท่ากับ x = x % 30 | 5 |
ตัวอย่างโค๊ด
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 สัญลักษณ์ตัวดำเนินการทางคณิตศาสตร์ ตรรกะ การเปรียบเทียบ และการกำหนดค่าพร้อมตัวอย่างพร้อมผลลัพธ์ หวังว่าคงพอจะมีประโยชน์สำหรับคนที่กำลังศึกษาบ้างไม่มากก็น้อยนะครับ.