Table of contents
- Declare Variables
- Boolean Variable
- Character Variable
- String Variable
- Multiline String
- Data Types
- Integer data types
- Real data types
- String data types
- Boolean data type
- Composite data types
- Pointers and Interfaces
- Pointers
- Interfaces
- 1. Plastic surgery (Integer Types)
- 2. Floating point (Float Types)
- 3. Plural (Complex Types)
- 4. Boolean (True/False)
- 5. Pointer (uintptr)
- 6. References (Composite Types)
- 7. Byte (Alias for uint8)
- 8. Any character (Rune)
- 9. String
- 10. Mistake (Error Handling)
- 1. Plastic Surgery (Integer Types) Lab
- 2. Floating Point Lab
- 3. Plural (Complex Types) Lab
- 4. Boolean Lab
- 5. Pointer Lab
- 6. References (Map, Slice, Channel) Lab
- 7. Byte and Rune Lab
- 8. String Lab
- 9. Error Lab
Declare Variables
Variable ကြေညာခြင်းဆိုသည်မှာ variable တစ်ခုကို ဖန်တီးပြီး အမည်နှင့် data type (အမျိုးအစား) ပေးခြင်း ဖြစ်သည်။ Go မှာ variable ကြေညာခြင်းကို
var
keyword နဲ့လုပ်နိုင်သလို:=
operator ဖြင့် အသုံးပြုနိုင်သည်။
ပထမနည်းလမ်းဖြစ်သည့် var
keyword ကိုအသုံးပြုပြီး Go တွင် variable များကြေငြာခြင်းအတွက် syntax သည်
var variable_name data_type
အထက်ပါ syntax ကို Separate variable declaration and assignment ဆိုတာကို ကြည့်ကျရအောင်
// define var then assign value
var num1 int
num1 = 5
var num1 int
declares a variablenum1
of typeint
.num1 = 5
assigns the value5
tonum1
.
Define variable and assignment in one line ဆိုတာကိုလဲ ကြည့်ရအောင်
var variable_name data_type = value
// define var and assign in 1 line
var num2 uint64 = 12345
- This line declares and assigns a value
12345
tonum2
of typeuint64
in a single statement
ဒုတိယနည်းလမ်းဖြစ်သည့် :=
operator ကိုအသုံးပြုပြီး Go တွင် variable များကြေငြာခြင်းအတွက် syntax သည်
variable_name := initial_value
အထက်ပါ syntax ကို Type Inference with Short Declaration ဆိုတာကို ကြည့်ကျရအောင်
// define var, inferred type and assign as short way
num3 := 3.14
- The
:=
syntax infers the type based on the assigned value. Here,num3
is inferred asfloat64
.
Boolean Variable
myVar := true
myVar
ဆိုတာ Boolean (true/false) အမျိုးအစားဖြစ်ပြီး true
လို့ သတ်မှတ်ထားပါတယ်။
Character Variable
charVar := 'a'
charVar
က rune
(Unicode ကိုယ်စားပြုတဲ့ int32
တစ်ခု) အမျိုးအစားပါ။
String Variable
strVar := "some nice string here"
strVar
ဆိုတာ စာသား (string) အမျိုးအစားဖြစ်ပြီး စာကြောင်းတစ်ကြောင်းကို သိမ်းဆည်းထားပါတယ်။
Multiline String
str := `Multiline
string`
`
(backticks) ကို သုံးပြီး Multiline string ရေးနိုင်ပါတယ်။ ဒီလို string က ရေးသားထားတဲ့ပုံစံအတိုင်း (လိုင်းခွဲခြင်း၊ space စသည်တို့အပါအဝင်) သိမ်းထားပါတယ်။
Constants
const MAX int = 50
MAX
ဆိုတာ အကန့်အသတ်ရှိတဲ့ int
အမျိုးအစား constant ဖြစ်ပါတယ်။ Constant တစ်ခုကို ကြေညာပြီးတာနဲ့ ပြန်လဲလှယ်လို့မရပါဘူး။
Public and Private Variables
Go မှာ Variable visibility (မြင်နိုင်မှု) ကို variable ရဲ့ အကြောင်းအရာအမျိုးအစားပေါ်မူတည်ပြီး သတ်မှတ်ပါတယ်။
Public Variables: အကြီးစာလုံးနဲ့ စတဲ့ variable တွေ။
Private Variables: အသေးစာလုံးနဲ့ စတဲ့ variable တွေ။
var SomeVar int = 5 // Public (package အပြင်ကနေ အသုံးပြုနိုင်တယ်)
var someVar2 int = 6 // Private (package အတွင်းမှာပဲ အသုံးပြုနိုင်တယ်)
Lab: Practice Variable Declaration and Usage in Go
Code Example:
package main
import "fmt"
func main() {
// Separate declaration and assignment
var num1 int
num1 = 5
// Declaration and assignment in one line
var num2 uint64 = 12345
// Type inference with short declaration
num3 := 3.14
// Boolean variable
myVar := true
// Character variable
charVar := 'a'
// String variable
strVar := "some nice string here"
// Multiline string
str := `Multiline
string`
// Constant definition
const MAX int = 50
// Public and private variables (for demonstration, in the same file)
var SomeVar int = 5 // Public (hypothetical in a real package context)
var someVar2 int = 6 // Private
// Printing the variables
fmt.Println("num1:", num1)
fmt.Println("num2:", num2)
fmt.Println("num3:", num3)
fmt.Println("myVar:", myVar)
fmt.Println("charVar:", charVar)
fmt.Println("strVar:", strVar)
fmt.Println("str:", str)
fmt.Println("MAX:", MAX)
fmt.Println("SomeVar (public):", SomeVar)
fmt.Println("someVar2 (private):", someVar2)
}
Steps to Run:
Save the file as
variables_lab.go
.Run the program using with this command:
go run variables_lab.go
Data Types
Go programming language ၏ Data Types ဆိုသည်မှာ variable တစ်ခုတွင် သိမ်းဆည်းနိုင်သည့် data အမျိုးအစားကို သတ်မှတ်ရန် အသုံးပြုသည်။ ၎င်းသည် memory ကို ဘယ်လို allocation ပြုလုပ်ရမလဲဆိုတာနှင့် data ပေါ်တွင် ဆောင်ရွက်နိုင်သော operations များကိုလည်း သတ်မှတ်ပေးသည်။
Go language တွင် Data Types များကို အဓိက ၂ မျိုးခွဲထားသည်။
Basic Data Types
Composite Data Types တို့ဖြစ်သည်။
Basic Data Types
Basic data types တွင် integer, floating-point, boolean, string တို့ကဲ့သို့သော အသုံးများသော data structures များပါဝင်သည်။
type | Go variables | fmt output |
Plastic surgery/Integer Types | int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 | %d |
Real data/Floating point types | float32 float64 | %f %e %g |
plural | complex128 complex64 | %v |
Boolean | bool | %t |
pointer | uintptr | %d %p |
References | map slice channel | %v |
byte | byte | %d |
Any character | rune | %d |
String | string | %s |
mistake | error | %v |
Integer data types
Integer data types ဆိုသည်မှာ integer values ကိန်းပြည့်များကို ကိုယ်စားပြုသည်။ ၎င်းဟာ positive, negative, or zero လဲ ဖြစ်နိုင်သည်။ Go တွင် integer data types အမျိုးအစားများစွာရှိပြီး၊ ယင်းတို့သည် size (bit length) နှင့် ၎င်းတို့ကို signed or unsigned အပေါ်မူတည်၍ ကွဲပြားကြသည်။ အောက်တွင် အသေးစိတ်ရှင်းပြထားသောဇယားကို ကြည့်ရှုနိုင်ပါသည်။
Type | Description | Range |
int8 | 8-bit signed integers. | from -128 to 127 |
int16 | 16-bit signed integers. | from -32,768 to 32,767 |
int32 | 32-bit signed integers. | from -231 to 231-1 |
int64 | 64-bit signed integers. | from -263 to 263-1 |
int | 32-bit or 64-bit signed integers (depending on the platform). Used by default with integer values. | As int32 or int64 (depending on the platform) |
uint8 | 8-bit unsigned integers. | from 0 to 255 |
uint16 | 16-bit unsigned integers. | from 0 to 65,535 |
uint32 | 32-bit unsigned integers. | from 0 to 232-1 |
uint64 | 64-bit unsigned integers. | from 0 to 264-1 |
uint | 32-bit or 64-bit unsigned integers (depending on the platform). | As uint32 or uint64 (depending on the platform) |
Integer Data Types အပြင် Go တွင် byte
နှင့် rune
တို့လည်းရှိပြီး၊ ၎င်းတို့သည် uint8
နှင့် int32
တို့၏ alias များဖြစ်သည်။
byte
:uint8
၏ alias ဖြစ်ပြီး binary data ကို ကိုယ်စားပြုရန်အထူးသင့်လျော်သည်။rune
:int32
၏ alias ဖြစ်ပြီး Unicode code point များကို ကိုယ်စားပြုရန်အသုံးပြုသည်။
package main
import "fmt"
func main() {
// Signed integer
var x int16 = 25000 // 16-bit signed integer
fmt.Println("x =", x)
// Unsigned integer
var y uint16 = 63000 // 16-bit Unsigned integer
fmt.Println("y =", y)
// Byte (alias for uint8)
var z byte = 4
fmt.Println("z =", z)
// Rune (alias for int8)
var w rune = 120
fmt.Println("w =", w)
}
Output:
x = 25000
y = 63000
z = 4
w = 120
အထက်ပါ code တွင် signed integer, unsigned integer, binary data အတွက် alias ဖြစ်သည့် byte
နှင့် Unicode code point အတွက် alias ဖြစ်သည့် rune
များကို အသုံးပြုနည်းကို ဖော်ပြထားခဲ့သည်။
Real data types
Real data types (Floating-point types) ဆိုသည်မှာ အပိုင်းကိန်းဂဏန်းများ (fractional numbers) ကို သိမ်းဆည်းရန် အသုံးပြုသည်။ Go language သည် float32 နှင့် float64 support လုပ်သည်။
float32 သည် memory တွင် 4 byte အသုံးပြုပြီး -3.4028235E+38
မှ 3.4028235E+38
အတွင်းရှိ floating point နံပါတ်များကို ဒဿမ ၇ နေရာ၏ တိကျမှုဖြင့် သိမ်းဆည်းနိုင်သည်။
float64 သည် memory တွင် 8 byte အသုံးပြုပြီး -1.7976931348623157E+308
မှ 1.7976931348623157E+308
အတွင်းရှိ floating point နံပါတ်များကို ဒသမ ၁၅ နေရာ၏ တိကျမှုဖြင့် သိမ်းဆည်းနိုင်သည်။
Computer အတွင်း real numbers (တကယ့်နံပါတ်များ) ကို binary form (နှစ်လုံးဂဏန်းစနစ်) ဖြင့် ကိုယ်စားပြုထားသည်ကို နားလည်ရန် အရေးကြီးပါသည်။ ၎င်းကြောင့် တိကျမှုမရှိနိုင်ပါ။ အဆိုပါနံပါတ်များပေါ်တွင် လုပ်ဆောင်ချက်များပြုလုပ်ရာတွင် rounding errors (မကြာမီခြင်းအမှားများ) ဖြစ်ပေါ်နိုင်သည်။ ထို့ကြောင့် ၎င်းတို့နှင့် ဆက်စပ်၍ အလုပ်လုပ်ရာတွင်၊ ၎င်းတို့၏ computer အတွင်းရှိ representation ၏ အထူးသဖြင့်ကို အသိပေးသင့်ပြီး rounding errors များကိုရှောင်ရှားရန် လိုအပ်သော အချက်များကို ထည့်သွင်းစဉ်းစားသင့်သည်။
It is important to understand that real numbers in a computer are represented in binary form, and therefore may be inaccurate. This may result in rounding errors when performing operations on such numbers. Therefore, when working with them, it is important to take into account the peculiarities of their representation in a computer and take measures to avoid rounding errors.
Examples of declaring real variables:
package main
import "fmt"
func main() {
var x float32 = 3.14 // 32-bit floating point
var y float64 = 3.1415926535897 // 64-bit floating point
fmt.Println("x (float32):", x)
fmt.Println("y (float64):", y)
}
String data types
Go တွင် String data types သည် စာလုံးများ၏ စိတ်ကြိုက် အကွက်များ (character strings) ကို သိမ်းဆည်းရန် အသုံးပြုသည်။ String တစ်ခု၏ အစိတ်အပိုင်း တစ်ခုချင်းစီသည် memory တွင် byte အလိုက် ဆက်စပ်နေသည်။
Go တွင် Strings သည် immutable objects ဖြစ်သည်။ ဒါဆိုရင် string တစ်ခု ဖန်တီးပြီးနောက်၌၊ ၎င်း၏ အကြောင်းအရာများကို ပြောင်းလဲ၍မရပါ။ သို့သော်၊ သင့်အား string များစွာကို ပေါင်းစပ်ပြီး အသစ်သော string တစ်ခု ဖန်တီးနိုင်ပါသည်။
Go တွင် special characters တစ်ချို့လည်း ရှိပါသည်။ အချို့မှာ -
\n
- line feed or newline\t
- tab\"
is a double quote\'
is a single quote\\
is a backslash
ထို့အပြင် Unicode characters များကိုလည်း အသုံးပြုနိုင်ပါသည်။ ၎င်းကို ပြုလုပ်ရန် \uXXXX ဖော်မတ်ဖြင့် အက္ခရာတစ်လုံး၏ Unicode value ကို hexadecimal အဖြစ် \uXXXX ဆိုပြီးရေးနိုင်သည်။
String data types in Go are used to store character strings. Each character in a string represents a sequence of bytes in memory.
Strings are immutable objects in Go. This means that once a string is created, its contents cannot be changed. However, you can create a new string by combining several existing strings.
Go also has special characters. Some of them are:
\n
- line feed or newline\t
- tab\"
is a double quote\'
is a single quote\\
is a backslash
In addition, it is possible to use Unicode characters. To do this, you can use a sequence of characters in the \uXXXX
format, where XXXX
is the hexadecimal value of the character's Unicode.
Examples of declaring a string variable:
package main
import "fmt"
func main() {
var exampleText1 string = "Hello, user1!" // String ကိုကြေညာခြင်း
exampleText2 := "Hello, user2!" // Short declaration
fmt.Println(exampleText1)
fmt.Println(exampleText2)
}
Boolean data type
Go has a boolean data type, bool
, which can take the values true
and false
.
This data type can be used to store the values of logical expressions, for example in conditional if and switch statements, as well as in logical operators such as &&
(logical AND
), ||
(logical OR
) and !
(logical NOT
).
For example, we can use the boolean data type in the following code:
age := 21
isAdult := age >= 21
fmt.Println(isAdult)
When executing the code, the output will show true
.
Composite data types
Next, let's look at composite types in Go.
Arrays
Arrays in Go are used to store a fixed number of elements of the same type. The main difference from other languages is that here arrays are values, not references to data.
To define an array, you must specify the element type and the number of elements in square brackets. For example:
var exampleArr [4]int = [4]int{2, 5, 7, 9}
Array elements are accessed by index, starting from 0:
fmt.Println(exampleArr[0])
Arrays in Go also support iteration using a for
loop:
for i := 0; i < len(exampleArr); i++ {
fmt.Println(exampleArr[i])
}
Slices
Slices are a reference data type in Golang that represents a dynamic array of elements stored in memory. They are very similar to arrays, but unlike them, the slices' size can be adjusted during program execution, and they are references to data rather than values.
A slice is defined using the following syntax:
var slice []T
Where T
is the type of slice elements, and slice
is a variable that stores a reference to the slice.
An empty slice can be declared like this:
emptySlice := []int{}
To initialize a slice, you can use the make
function, which creates a new slice of a given length:
slice := make([]int, 5)
The example above creates a slice of five int
elements.
In addition, the make
function has a third optional parameter that denotes capacity. It is used to pre-specify the amount of memory that will be allocated to store slice elements. Due to the reduction in the number of memory allocation operations, the load on the processor is significantly reduced.
For example:
slice := make([]int, 10, 20)
This code snippet creates a slice containing 10 elements of the int
data type and with a pre-allocated capacity of 20 elements. This means that the slice initially contains 10 zero-initialized elements, and can be expanded to a maximum of 20 elements without the need to allocate additional memory. If suddenly the number of slice elements becomes more than 20, then Go will automatically allocate a new memory block that will be 2 times larger than the previous one.
The capacity approach is recommended when the user knows in advance the maximum number of elements that will need to be stored in the slice.
Indexing is used to access elements, just like with arrays.
Structures
In short, structures are a collection of fields. The fields, in turn, characterize various attributes of objects.
To declare a structure in Go, you use the keyword type
, followed by the name of the structure and its definition in curly braces. For example, we will declare a structure representing information about a person:
type Person struct {
Name string
Age int
Address string
}
To create a variable that stores the values of a structure, you need to use the var keyword and the name of the variable. The variable name is then followed by the structure name, followed by curly braces with the structure field values in the form field_name: value
. For example:
var person1 Person
person1 = Person{Name: "John", Age: 30, Address: "123 Main St."}
Here we declare a variable person1
of the Person
type and assign it values for each field. You can also use a short form of structure initialization:
person2 := Person{Name: "Jane", Age: 25, Address: "456 Elm St."}
Structure fields are accessed using the dot (.
) operator. For example, to get the name person1
, you need to write:
person1.Name
Structures can contain other structures within themselves and even references to themselves (so-called recursive structures). In addition, they can implement interfaces.
Maps
A map is a reference data type that is used to represent an unordered set of key-value pairs. The keys in the map must be unique, but the values may be repeated.
To create a map, the built-in make()
function is used, which is passed the key and value types. The following code creates an empty map with string
keys and int
values:
m := make(map[string]int)
To add an element to a map, you can use the following entry form:
m[key] = value
For example:
m["apple"] = 3
m["banana"] = 5
To get the value of an element by key, use the following notation form:
value := m[key]
For example, to get the value of an element with the key apple
:
count := m["apple"]
If an element with the specified key is not in the map, the default value will be returned.
To remove an element from a map, you can use the built-in delete()
function. The following code removes the element with the key apple
:
delete(m, "apple")
In Go, maps are implemented as hash tables, so the order of elements in a map is not fixed and can change each time the program is executed. You should also pay attention to the fact that the map is not thread-safe, therefore, when accessing the map in parallel, you must use access synchronization mechanisms (mutexes or channels).
Pointers and Interfaces
Pointers
Pointers are needed to interact with variables that store addresses in memory. They can be defined by using the &
symbol in front of the variable, and the *
symbol is used to get the value pointed to by the pointer.
Example:
var myInt int = 42
var myIntPointer *int = &myInt
fmt.Println(*myIntPointer)
The output of the program is shown in the picture below.
Interfaces
Interfaces are used to define a set of methods that a data type must implement. Interfaces can be used to abstract away a specific data type and to implement polymorphism. To create an interface in Go, you need to define a set of methods that the data type must implement.
Example:
type Writer interface {
Write([]byte) (int, error)
}
This defines the Writer
interface, which must have a Write
method with an argument of type []byte
and return the number of bytes written and an error.
Go programming language အတွက် Variable Types အပိုင်းကို အသေးစိတ်ရှင်းပြပါမယ်—
1. Plastic surgery (Integer Types)
Go Variables:
int
,int8
,int16
,int32
,int64
,uint
,uint8
,uint16
,uint32
,uint64
အသုံးပြုမှု: Integer type တွေကို ဆောင်ရွက်ရာမှာ အသုံးပြုပါတယ်။
int
က platform မှာသက်ဆိုင်တဲ့ size ကို အသုံးပြုပြီး၊int8
,int16
,int32
,int64
ဆိုရင် bit size အတိအကျသတ်မှတ်ထားတဲ့ integer တွေ ဖြစ်ပါတယ်။fmt Output:
%d
(decimal number format)ဥပမာ:
int age = 25; fmt.Printf("%d", age)
ဆိုရင်25
လို့ output ထွက်ပါမယ်။
2. Floating point (Float Types)
Go Variables:
float32
,float64
အသုံးပြုမှု: ရိုးရှင်းတဲ့ သိုလှောင်ရေတွက်ချက်တွေ အတွက် floating point numbers (ပေါင်းသင်္ချာအတွက် သတ်မှတ်မဟုတ်တဲ့ ဂဏန်းတစ်ခု) ကို သုံးပါတယ်။
fmt Output:
%f
,%e
,%g
(floating-point format)ဥပမာ:
float64 pi = 3.14159; fmt.Printf("%f", pi)
ဆိုရင်3.141590
လို့ output ထွက်ပါမယ်။
3. Plural (Complex Types)
Go Variables:
complex64
,complex128
အသုံးပြုမှု: အစစ်နှင့် အိမ်ရှင် အစိတ်အစများပါဝင်တဲ့ complex numbers တွေကို သုံးတဲ့အခါ အသုံးပြုပါတယ်။
fmt Output:
%v
(default format)ဥပမာ:
complex128 c = 1 + 2i; fmt.Printf("%v", c)
ဆိုရင်1+2i
လို့ output ထွက်ပါမယ်။
4. Boolean (True/False)
Go Variables:
bool
အသုံးပြုမှု: တစ်ခုခုကို
true
နဲ့false
အဖြစ် သတ်မှတ်ထားတဲ့ logical values အတွက်အသုံးပြုပါတယ်။fmt Output:
%t
(boolean format)ဥပမာ:
bool isActive = true; fmt.Printf("%t", isActive)
ဆိုရင်true
လို့ output ထွက်ပါမယ်။
5. Pointer (uintptr)
Go Variables:
uintptr
အသုံးပြုမှု: Pointer type တွေကို memory address ကို ကိုင်တွယ်ဖို့အသုံးပြုပါတယ်။
uintptr
က memory address ကို integer အနေနဲ့ပြထားတာပါ။fmt Output:
%d
,%p
(pointer format)ဥပမာ:
var ptr uintptr = uintptr(unsafe.Pointer(&someVar)); fmt.Printf("%p", ptr)
ဆိုရင် memory address ကို output ထုတ်ပါမယ်။
6. References (Composite Types)
Go Variables:
map
,slice
,channel
အသုံးပြုမှု: Data collections (
map
for key-value pairs,slice
for dynamic arrays,channel
for concurrent programming) ကို ကိုင်တွယ်ဖို့အသုံးပြုပါတယ်။fmt Output:
%v
(default format)ဥပမာ:
var cities = map[string]string{"MM": "Yangon"}; fmt.Printf("%v", cities)
ဆိုရင်map[MM:Yangon]
လို့ output ထွက်ပါမယ်။
7. Byte (Alias for uint8)
Go Variables:
byte
အသုံးပြုမှု: Single-byte data ကို သိုလှောင်ဖို့အသုံးပြုပါတယ်။
fmt Output:
%d
(decimal format)ဥပမာ:
byte char = 'A'; fmt.Printf("%d", char)
ဆိုရင် ASCII value65
ထွက်ပါမယ်။
8. Any character (Rune)
Go Variables:
rune
အသုံးပြုမှု: Unicode code point ကို သတ်မှတ်ဖို့ အသုံးပြုပါတယ်။
rune
ကint32
ကို အဓိပ္ပါယ်ရထားပါတယ်။fmt Output:
%d
(decimal format)ဥပမာ:
rune letter = 'ဂ'; fmt.Printf("%d", letter)
ဆိုရင် Unicode value ထွက်ပါမယ်။
9. String
Go Variables:
string
အသုံးပြုမှု: Text data ကို သိုလှောင်ဖို့အသုံးပြုပါတယ်။
fmt Output:
%s
(string format)ဥပမာ:
string message = "Hello, Go!"; fmt.Printf("%s", message)
ဆိုရင်Hello, Go!
လို့ output ထွက်ပါမယ်။
10. Mistake (Error Handling)
Go Variables:
error
အသုံးပြုမှု: Error handling အတွက် Go မှာ
error
interface ကို အသုံးပြုပါတယ်။fmt Output:
%v
(default format)ဥပမာ:
var err error =
errors.New
("Something went wrong"); fmt.Printf("%v", err)
ဆိုရင်Something went wrong
လို့ output ထွက်ပါမယ်။
ဒီ table က Go programming language မှာ variable type တွေရဲ့ သုံးစွဲပုံနဲ့ output format ကို ဖော်ပြထားတဲ့အတွက်၊ Go developer တွေအတွက် အလွန် အသုံးဝင်ပါတယ်။
အကောင်းဆုံးဖြစ်အောင် Labs တွေနဲ့ Go Programming Language Variable Types ကို ရှင်းပြပါမယ်။
1. Plastic Surgery (Integer Types) Lab
Objective: Integer types တွေကို ပြောင်းလဲသုံးပြီး, fmt.Printf
ကို အသုံးပြုပြီး output ထုတ်ပါ။
Lab Example:
package main
import "fmt"
func main() {
var a int = 42
var b int8 = 120
var c int16 = 30000
var d uint = 50000
fmt.Printf("Integer a: %d\n", a)
fmt.Printf("Integer b (int8): %d\n", b)
fmt.Printf("Integer c (int16): %d\n", c)
fmt.Printf("Unsigned integer d (uint): %d\n", d)
}
Lab Result: Output က integer value တွေကို decimal format နဲ့ ပြပါမယ်။
2. Floating Point Lab
Objective: Floating point numbers တွေကို သုံးပြီး fmt.Printf
format specifier %f
, %e
, %g
တွေကို ကြည့်ပါ။
Lab Example:
package main
import "fmt"
func main() {
var pi float64 = 3.14159265359
var num float32 = 2.71828
fmt.Printf("Pi in decimal format: %f\n", pi)
fmt.Printf("Pi in scientific notation: %e\n", pi)
fmt.Printf("Number (num) in shortest representation: %g\n", num)
}
Lab Result: Decimal format, scientific notation, နဲ့ shortest representation အနေနဲ့ output ကို ကြည့်ပါ။
3. Plural (Complex Types) Lab
Objective: Complex number types (complex64
, complex128
) ကို သုံးပြီး စမ်းကြည့်ပါ။
Lab Example:
package main
import "fmt"
func main() {
var complexNum complex128 = 1 + 2i
fmt.Printf("Complex Number: %v\n", complexNum)
fmt.Printf("Real Part: %f, Imaginary Part: %f\n", real(complexNum), imag(complexNum))
}
Lab Result: Output မှာ complex number နဲ့ ထို number ရဲ့ real နဲ့ imaginary parts တွေကို ထုတ်ပြပါမယ်။
4. Boolean Lab
Objective: Boolean types (bool
) ကို အသုံးပြုပြီး logic check လုပ်ပါ။
Lab Example:
package main
import "fmt"
func main() {
var isGoFun bool = true
var isHard bool = false
fmt.Printf("Is Go fun? %t\n", isGoFun)
fmt.Printf("Is Go hard? %t\n", isHard)
}
Lab Result: Boolean values တွေကို true/false အနေနဲ့ output ထုတ်ပါမယ်။
5. Pointer Lab
Objective: uintptr
ကို memory address တွေကို output ထုတ်ဖို့သုံးပါ။
Lab Example:
package main
import "fmt"
func main() {
var num int = 42
var ptr *int = &num
fmt.Printf("Memory address of num: %p\n", ptr)
fmt.Printf("Value at the pointer: %d\n", *ptr)
}
Lab Result: Memory address နဲ့ pointer ရဲ့ value ကို output ထုတ်ပါမယ်။
6. References (Map, Slice, Channel) Lab
Objective: Map, slice, နဲ့ channel တွေကို သုံးပြီး output format %v
ကို စမ်းပါ။
Lab Example:
package main
import "fmt"
func main() {
// Map
cities := map[string]string{"MM": "Yangon", "SG": "Singapore"}
fmt.Printf("Cities map: %v\n", cities)
// Slice
numbers := []int{1, 2, 3, 4, 5}
fmt.Printf("Numbers slice: %v\n", numbers)
}
Lab Result: Map နဲ့ slice ကို %v
format နဲ့ output ထုတ်ပါမယ်။
7. Byte and Rune Lab
Objective: byte
နဲ့ rune
type တွေကို Unicode နဲ့ ASCII representation အနေနဲ့ output ထုတ်ပါ။
Lab Example:
package main
import "fmt"
func main() {
var letter byte = 'A'
var symbol rune = 'ဂ'
fmt.Printf("Byte (ASCII) of 'A': %d\n", letter)
fmt.Printf("Rune (Unicode) of 'ဂ': %d\n", symbol)
}
Lab Result: byte
နဲ့ rune
တို့ရဲ့ ASCII နဲ့ Unicode value ကို output ပြပါမယ်။
8. String Lab
Objective: String ကို format specifier %s
နဲ့ output ထုတ်ပါ။
Lab Example:
package main
import "fmt"
func main() {
var greeting string = "Hello, Go!"
fmt.Printf("Greeting: %s\n", greeting)
}
Lab Result: String ကို တိုက်ရိုက် output ထုတ်ပါမယ်။
9. Error Lab
Objective: error
type ကို သုံးပြီး error message ကို output ထုတ်ပါ။
Lab Example:
package main
import (
"fmt"
"errors"
)
func main() {
var err error = errors.New("Something went wrong")
fmt.Printf("Error: %v\n", err)
}
Lab Result: Error message ကို output ထုတ်ပါမယ်။
ဒီ Labs တွေက Go programming language variable types တွေကို သင်ယူစမ်းသပ်ဖို့ အကောင်းဆုံးဖြစ်စေပါတယ်။