Go ด้วยกัน Go ได้ไกล- Print, Operations, Variables, Data Types [EP.2]

Grassroot Engineer
8 min readAug 3, 2024

--

https://www.softwebsolutions.com/resources/golang-web-development.html

From EP1 นะคับ
มาต่อกันนะคับ สำหรับ Basic Go ที่ต้องรู้

  1. Print and operations
  • หลักๆจะคล้ายๆกับภาษาอื่นอยู่แล้วนะคับ ลองทำความเข้าใจจากตัวอย่างด้านล่างได้เลยครับ
package main

import "fmt"

func main() {
fmt.Println("---------------------")
fmt.Println("Hello, World!")
fmt.Println("---------------------")
fmt.Print("This is sample code for Go programming language.")
fmt.Print("for the beginners.\n")
fmt.Println("This is the end of the code.")

fmt.Println("---------- Operation -----------")
fmt.Println(2 + 3) // addition
fmt.Println(2 - 3) // subtraction
fmt.Println(2 * 3) // multiplication
fmt.Println(2 / 3) // division
fmt.Println(2 % 3) //modulus

fmt.Println("---------- True False -----------")
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
fmt.Println(!false)

fmt.Println("----------- Bitwise ----------")
fmt.Println(2 & 3) // bitwise AND
fmt.Println(2 | 3) // bitwise OR
fmt.Println(2 ^ 3) // bitwise XOR
fmt.Println(2 &^ 3) // bitwise AND NOT
fmt.Println(2 << 1) // left shift
fmt.Println(2 >> 1) // right shift
}
print and operation
  • จำไว้เลยนะคับ print ที่เราใช้กันจะมี 3 แบบคือ
    1. Println() = มี \n ให้แล้ว
    2. Print() = ไม่มี \n ให้ (ไม่ขึ้นบรรทัดใหม่)
    3. Printf() = print format คล้ายๆกับ Python
Printf(), Sprint()
  • ด้านล่างนี้ผมรวบรวม ref ทั้งหมดของ Printf() ที่ใช้งานได้มานะคับ
 // รหัสของรูปแบบต่างๆ ของ format specifier
// %v คือ format specifier สำหรับ value ใดๆ ที่เป็นไปได้
// %T คือ format specifier สำหรับชนิดของข้อมูล
// %t คือ format specifier สำหรับ boolean
// %d คือ format specifier สำหรับ integer
// %b คือ format specifier สำหรับ binary
// %o คือ format specifier สำหรับ octal
// %x คือ format specifier สำหรับ hexadecimal
// %X คือ format specifier สำหรับ hexadecimal ใหญ่
// %f คือ format specifier สำหรับ float
// %e คือ format specifier สำหรับ scientific notation
// %E คือ format specifier สำหรับ scientific notation ใหญ่
// %s คือ format specifier สำหรับ string
// %q คือ format specifier สำหรับ quoted string
// %p คือ format specifier สำหรับ pointer
// %c คือ format specifier สำหรับ character
// %U คือ format specifier สำหรับ Unicode
// %v คือ format specifier สำหรับ value ใดๆ ที่เป็นไปได้

// example
fmt.Printf("%v\n", 100) // 100 // v คือ ค่าของตัวแปร
fmt.Printf("%T\n", 100) // int // T ใหญ่คือ type นะ (type ของ 100 คือ int)
fmt.Printf("%t\n", true) // true
fmt.Printf("%d\n", 100) // 100
fmt.Printf("%b\n", 100) // 1100100
fmt.Printf("%o\n", 100) // 144
fmt.Printf("%x\n", 100) // 64 // รูปแบบเลขฐาน 16
fmt.Printf("%X\n", 100) // 64 // รูปแบบเลขฐาน 16 เช่นกัน
fmt.Printf("%f\n", 100.123) // 100.123000
fmt.Printf("%e\n", 100.123) // 1.001230e+02
fmt.Printf("%E\n", 100.123) // 1.001230E+02
fmt.Printf("%s\n", "Hello, World!") // Hello, World!
fmt.Printf("%q\n", "Hello, World!") // "Hello, World!" // พิมพ์เครื่องหมาย " ครอบข้อความ
fmt.Printf("%p\n", &s) // 0xc0000b6010
fmt.Printf("%c\n", 100) // d
fmt.Printf("%U\n", 100) // U+0064

println("--------------------")

// การใช้งานรูปแบบการแสดงผลต่าง ๆ ด้วย Printf
fmt.Printf("%d\n", 42) // ผลลัพธ์: 42
fmt.Printf("%+d\n", 42) // ผลลัพธ์: +42
fmt.Printf("%5d\n", 42) // ผลลัพธ์: 42
fmt.Printf("%-5d\n", 42) // ผลลัพธ์: 42
fmt.Printf("%05d\n", 42) // ผลลัพธ์: 00042
fmt.Printf("%b\n", 5) // ผลลัพธ์: 101
fmt.Printf("%o\n", 10) // ผลลัพธ์: 12
fmt.Printf("%#o\n", 10) // ผลลัพธ์: 012
fmt.Printf("%x\n", 255) // ผลลัพธ์: ff
fmt.Printf("%X\n", 255) // ผลลัพธ์: FF
fmt.Printf("%U\n", 'A') // ผลลัพธ์: U+0041 // แสดงรหัส Unicode
fmt.Printf("%#U\n", 'A') // ผลลัพธ์: U+0041 'A' // แสดงรหัส Unicode และ ตัวอักษร
fmt.Printf("%c\n", 65) // ผลลัพธ์: A // แสดงตัวอักษรที่แทนด้วยรหัส Unicode
fmt.Printf("%q\n", "Hello") // ผลลัพธ์: "Hello" // แสดง string แบบที่มี " ครอบ
fmt.Printf("%t\n", true) // ผลลัพธ์: true // พิมพ์ค่า boolean
fmt.Printf("%f\n", 123.456) // ผลลัพธ์: 123.456000 // พิมพ์ค่า float ตามมาตรฐาน
fmt.Printf("%.2f\n", 123.456) // ผลลัพธ์: 123.46 // พิมพ์ค่า float แบบทศนิยม 2 ตำแหน่ง
fmt.Printf("%9.2f\n", 123.456) // ผลลัพธ์: 123.46 // พิมพ์ค่า float แบบทศนิยม 2 ตำแหน่ง และ ตำแหน่งทั้งหมด 9 ตำแหน่ง
fmt.Printf("%9.f\n", 123.456) // ผลลัพธ์: 123
fmt.Printf("%g\n", 123.456) // ผลลัพธ์: 123.456
fmt.Printf("%G\n", 123.456) // ผลลัพธ์: 123.456
fmt.Printf("%e\n", 123.456) // ผลลัพธ์: 1.234560e+02 // แสดงเป็นรูปแบบ scientific notation (รูปแบบ exponential)
fmt.Printf("%s\n", "Hello") // ผลลัพธ์: Hello // พิมพ์ string แบบปกติเลยนะ
fmt.Printf("%10s\n", "Hello") // ผลลัพธ์: Hello // %10s คือ String 10 ตำแหน่งนะ ถ้าไม่ครบจะเติมช่องว่างด้านซ้าย
fmt.Printf("%-10s\n", "Hello") // ผลลัพธ์: Hello // ถ้าไม่ถึงจะเติมช่องว่างด้านขวา
fmt.Printf("%T\n", 123) // ผลลัพธ์: int
fmt.Printf("%v\n", 123) // ผลลัพธ์: 123
fmt.Printf("%%\n")

2. Data types

นี่คือการสร้าง variable ใน Go นะ
var love int8 = 10 หรือ love := 3000 ทำได้ทั้ง 2 แบบนะ

  • ลองอ่านกันเล่นๆนะคับ
package main

import (
"fmt"
"html"
)

// Glabal variable
var xml = "outside-myxml"

func main() {

// Local variable
xml := "inside-myxml"
fmt.Println(xml) // EscapeString: myxml

// Data type in Go
// 2 types of data type in Go
// 1. Primitive data type (basic data type)
// 2. Derived data type (composite data type)

// Numeric data type
// - Integer data type (จำนวนเต็ม)
var a int8 = 10 // range: -128 to 127
var b int16 = 20 // range: -32768 to 32767
var d int64 = 40 // range: -9223372036854775808
var e uint8 = 50 // range: 0 to 255
var f uint16 = 60 // range: 0 to 65535
var g uint32 = 70 // range: 0 to 4294967295
var h uint64 = 80 // range: 0 to 18446744073709551615
var i int = 90 // int is alias of int32 or int64
var j uint = 100 // uint is alias of uint32 or uint64
var k uintptr = 110 // uintptr is an unsigned integer type that is large enough to store the uninterpreted bits of a pointer value
var l byte = 120 // byte is alias of uint8
var m rune = 130 // rune is alias of int32

// - Floating-point data type (จำนวนทศนิยม)
var n float32 = 3.14
var o float64 = 3.141592653589793

// - Complex data type (จำนวนเชิงซ้อน)
var p complex64 = 3 + 4i
var q complex128 = 5 + 6i

// Boolean data type
var r bool = true
var s bool = false

// String data type (ข้อความ)
var t string = "Hello, World!" // string literal
var u string = `Hello,
World!` // raw string literal

// Escape sequence (อักขระพิเศษ พวก backslash ต่างๆนะ พวกนี้จะใส่ใน print ต่างๆนะ)
// \a Bell (alert) (เสียงเตือน)
// \b Backspace (ลบเครื่องหมาย)
// \f Form feed (ขึ้นบรรทัดใหม่)
// \n New line (ขึ้นบรรทัดใหม่)
// \r Carriage return (เลื่อนเคอร์เซอร์ไปตำแหน่งแรกของบรรทัด)
// \t Horizontal tab (เว้นวรรคแนวนอน)
// \v Vertical tab (เว้นวรรคแนวตั้ง)
// \' Single quote (เครื่องหมายอัญประสงค์เดี่ยว)
// \" Double quote (เครื่องหมายอัญประสงค์คู่)
// \\ Backslash (เครื่องหมายเบ็ดเสร็จ)

println(a, b, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u)

const str = `<a href="email@example.com">
Send Email</a>` // raw string literal แบบนี้จะใส่ได้หลายบรรทัดนะ
en := html.EscapeString(str) // escape string
de := html.UnescapeString(en) // unescape string

fmt.Println("Encoded: ", en)
fmt.Println("Decoded: ", de)

}
Result ที่ได้มาจาก go run

3. Variables

  • การสร้างตัวแปรใน Go ทำได้ 2 แบบ ที่ได้เขียนไว้ก่อนหน้านี้แล้วนะคับ อธิบายเพิ่มเติมดังนี้คือ

3.1 Long declaration — ประกาศด้วย var ซึ่งเป็นจะระบุ type หรือ ไม่ก็ได้นะเช่น

var x int  //ประกาศแบบนี้ค่าเริ่มต้นเป็น 0 นะ
var y int = 10 //ระบุแบบนี้มี 10 เป็นค่าเริ่มต้น
var z = 20 // แบบนี้ไม่ได้ระบุ type แต่ Go จะ infer เป็น int ให้อัตโนมัติเลย ค่าเริ่มต้นคือ 20

3.2 Short declaration — ประกาศแบบนี้จะใช้ := และไม่ต้องใส่ var ซึ่งเหมาะกับการใช้งานภายใน function เท่านั้น (ไม่งั้นพังจ้า)

a := 10  // ประกาศ a กำหนดให้เป็น 10 เลย (Go จะ infer type ให้เองเลยนะ)
b, c := 20, 30 // ประกาศ b, c พร้อมค่าเริ่มต้น คือ ประกาศทีละหลายตัวก็ได้นั่นเอง

3.3. ประกาศตัวแปรหลายตัวในครั้งเดียว แบบใส่วงเล็บ

var (
i int // ประกาศ i เป็น int
j = 20 // ประกาศ j ไม่ระบุ type แต่ให้เป็น 20 (Go จะ infer เป็น int)
k string // ประกาศ k เป็น string แต่ไม่กำหนดค่าเริ่มต้น (Go จะให้เป้น "" นะ)
)

3.4 ประกาศตัวแปรแบบ Constant
(จะต้องมี const นำหน้านะ ซึ่งไม่สามารถเปลี่ยนได้หลังจากประกาศแล้ว)

const pi = 3.14  // ประกาศ const pi เป็น float64
const greeting = "Hello" // ประกาศ const greeting เป็น string "Hello"

3.5 Naming Conventions — กฎการตั้งชื่อตัวแปร

  • ให้สื่อความหมาย เช่น age, height, studentName
  • ใช้ camelCase (เหมือนกับ Flutter นะ) เช่น studentID, firstName
  • ใช้ PascalCase สำหรับตัวแปรที่เป็น public หรือ exported คือ ตัวแปรที่ต้องการให้ access จากภายนอก package ได้ เช่น StudentID, FirstName

3.6 Reserved words in Go (มีแค่ 25 คำเองนะ)

https://www.scaler.com/topics/golang/golang-identifiers-variables-constant-keywords/
// 1. short variable declaration
// <variable_name> := <value>
product := "Computer"
price := 10000
quantity := 10

fmt.Println(product)
fmt.Println(price)
fmt.Println(quantity)

fmt.Println("--------------------")


// 2. Multiple short variable declaration
// <variable_name1>, <variable_name2> := <value1>, <value2>
var x, y, z = 10, 20, 30
var i, j, k = 10.5, 20.5, 30.5
var p, q, r = "Hello", "World", "Go"


// 3. Constant
// ประกาศค่าคงที่
// const <constant_name> <data_type> = <value>
const pi float32 = 3.14
const vat = 7.0
const name1, name2 = "John", "Doe"
// vat = 10.0 // error: cannot assign to vat


// 4. Variable scope
// ตัวแปรที่ประกาศใน function จะมีขอบเขตใน function นั้นๆ
// ตัวแปรที่ประกาศใน block จะมีขอบเขตใน block นั้นๆ
// ตัวแปรที่ประกาศด้วย var จะมีขอบเขตใน function นั้นๆ
// ตัวแปรที่ประกาศด้วย := จะมีขอบเขตใน block นั้นๆ

// ตัวอย่างขอบเขตของตัวแปร
var mywordLocal string = "Awesome Local Variable"
fmt.Println(myworldGlob) // แสดงค่าตัวแปร myword ที่ประกาศใน package main
fmt.Println(mywordLocal) // แสดงค่าตัวแปร myword ที่ประกาศใน function main

4. Type Conversion

Ex:

  • int to float 64
// แปลงจากจำนวนเต็มเป็นจำนวนทศนิยม
package main

import (
"fmt"
)

func TypeConversion1() {
var i int = 42
var f float64 = float64(i)
fmt.Printf("i: %d, f: %f\n", i, f)
// ผลลัพธ์: i: 42, f: 42.000000
}
  • float to int
// แปลงจากจำนวนทศนิยมเป็นจำนวนเต็ม

package main

import (
"fmt"
)

func TypeConversion2() {
var f float64 = 42.56
var i int = int(f)
fmt.Printf("f: %f, i: %d\n", f, i)
// ผลลัพธ์: f: 42.560000, i: 42
}
  • int to string
// แปลงจากจำนวนเต็มเป็นสตริง

package main

import (
"fmt"
"strconv"
)

func TypeConversion3() {
var i int = 42
var s string = strconv.Itoa(i)
fmt.Printf("i: %d, s: %s\n", i, s)
// ผลลัพธ์: i: 42, s: 42
}
  • string to int
// แปลงจากสตริงเป็นจำนวนเต็ม

package main

import (
"fmt"
"strconv"
)

var s string = "42"
var i int
var err error
i, err = strconv.Atoi(s)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Printf("s: %s, i: %d\n", s, i)
// ผลลัพธ์: s: 42, i: 42
  • int to bool (เช็ค int ก่อนถ้าไม่เท่ากับ 0 ค่อยมาแปลงต่อนะ)
// แปลงจากจำนวนเต็มเป็นบูลีน (แสดงในรูปแบบ integer เพราะ Go ไม่รองรับการแปลงจาก int เป็น bool โดยตรง)

package main

import "fmt"

func TypeConversion5() {
var i int = 1
var b bool = (i != 0)
fmt.Printf("i: %d, b: %t\n", i, b)
// ผลลัพธ์: i: 1, b: true
}
  • bool to int
// แปลงจากบูลีนเป็นจำนวนเต็ม (แสดงในรูปแบบ integer เพราะ Go ไม่รองรับการแปลงจาก bool เป็น int โดยตรง)

package main

import "fmt"

func TypeConversion6() {
var b bool = true
var i int
if b {
i = 1
} else {
i = 0
}
fmt.Printf("b: %t, i: %d\n", b, i)
// ผลลัพธ์: b: true, i: 1
}

If you think it’s useful for you, just clap your hands 👏 to be encouraged me.

GRASSROOT ENGINEER 😘

แล้วพบกันใหม่ตอนถัดไปคับ

--

--

Grassroot Engineer
Grassroot Engineer

Written by Grassroot Engineer

ATM engineer who is interested in CODING and believe in EFFORT. — https://grassrootengineer.com

No responses yet