1. Your First Go Program
package main
import "fmt"
func main() {
a := 10
fmt.Println(a)
fmt.Println("Hello Sajjad in the GO..!")
}
Step-by-step:
-
package main
β Every Go file belongs to a package.
-
main
is special β itβs where the program starts.
-
import "fmt"
β Lets us use extra tools from the Go library.
-
"fmt"
is for printing text/numbers.
func main() { ... }
β This is the starting point of your program.a := 10
β Create a variablea
and put10
in it.
-
:=
means create and set value (Go figures out type automatically).
fmt.Println(a)
β Print value ofa
with a new line.fmt.Println("Hello Sajjad in the GO..!")
β Print text.
2. :=
vs =
β The Easy Difference
Symbol | Meaning | Use When |
---|---|---|
:= |
Create a new variable and set a value | First time you make the variable |
= |
Change the value of an existing variable | The variable already exists |
Example:
func main() {
age := 20 // Create a new variable 'age' (first time)
age = 25 // Change value of 'age'
name := "Sajjad" // Create variable
name = "Rahman" // Change value
}
β Wrong:
func main() {
number = 5 // ERROR: number doesn't exist yet
}
β
Right:
func main() {
number := 5 // First time β use :=
number = 10 // Already exists β use =
}
3. Data Types in Go
Go has different types for different kinds of data.
A. Numbers
1. Integers (whole numbers)
-
int
β Default whole number type. -
int8
β Very small number (from -128 to 127). -
uint
β Positive whole number (no negatives).
func main() {
var age int = 25 // normal number
var small int8 = -100 // tiny number
var big uint = 500 // positive only
fmt.Println(age, small, big)
}
2. Decimal Numbers
-
float32
β Decimal numbers (less precision). -
float64
β Decimal numbers (more precision, most used).
func main() {
var price float32 = 9.99
var pi float64 = 3.1415926535
fmt.Println(price, pi)
}
B. Boolean
- Only
true
orfalse
.
func main() {
var isOnline bool = true
var isFinished bool = false
fmt.Println(isOnline, isFinished)
}
C. String
- Text inside double quotes
"..."
.
func main() {
var name string = "Sajjad Rahman"
fmt.Println(name)
}
D. Rune
- Single character (Unicode) inside single quotes
'...'
.
func main() {
var letter rune = 'A'
fmt.Println(letter) // prints 65 (ASCII code)
fmt.Printf("%c\n", letter) // prints A
}
E. Byte
- Same as
uint8
, stores small numbers or raw data.
func main() {
var b byte = 65
fmt.Println(b) // 65
fmt.Printf("%c\n", b) // A
}
4. Declaring Variables
There are 3 ways:
1. Normal declaration
var age int = 25
2. Type inference (Go guesses type)
var age = 25 // Go knows it's int
3. Short declaration
age := 25 // only inside functions
5. Constants
- Values that never change.
const Pi = 3.14159
6. Zero Values
If you make a variable without setting it, Go gives it a default value:
Type | Zero Value |
---|---|
int | 0 |
float | 0.0 |
bool | false |
string | "" |
func main() {
var number int
var text string
var check bool
fmt.Println(number, text, check) // 0 "" false
}
7. Full Example Using All Types
package main
import "fmt"
func main() {
// Integer
age := 30
// Float
price := 19.99
// Boolean
isMember := true
// String
name := "Sajjad"
// Rune
letter := 'A'
// Byte
code := byte(66) // ASCII for 'B'
fmt.Println(age, price, isMember, name)
fmt.Printf("Rune: %c, Byte: %c\n", letter, code)
}
Top comments (1)
The way you explained
:=
vs=
and broke down data types with examples makes Go feel way less intimidating. I especially liked the use ofrune
andbyte
not every intro covers those, and theyβre super useful when working with Unicode or raw data.