Contents
Overview
Defination and Syntax
Go has a clean and straightforward syntax that resembles C, but with improvements that facilitate more efficient programming. Here’s an example of a simple Go program:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Data Types
Go provides several built-in data types, including numeric types, strings, booleans, and more complex data structures like arrays, slices, and maps.
Basic Data Types
- Integers:
int
,int8
,int16
,int32
,int64
,uint
,uint8
,uint16
,uint32
,uint64
- Floating-point numbers:
float32
,float64
- Complex numbers:
complex64
,complex128
- Boolean:
bool
(true
orfalse
) - String:
string
Example of Basic Data Types
package main
import "fmt"
func main() {
var a int = 42
var b float64 = 3.14
var c bool = true
var d string = "Hello, Go!"
fmt.Println(a, b, c, d)
}
Derived Data Types
- Arrays: Fixed-size collections of elements of the same type.
- Slices: Dynamic arrays that can grow and shrink in size.
- Maps: Collections of key-value pairs.
- Structs: Custom data types that group fields together.
Example of Derived Data Types
package main
import "fmt"
func main() {
// Array
var numbers [3]int = [3]int{1, 2, 3}
// Slice
var fruits []string = []string{"apple", "banana", "cherry"}
// Map
colors := map[string]string{"red": "#FF0000", "green": "#00FF00", "blue": "#0000FF"}
// Struct
type Person struct {
Name string
Age int
}
person := Person{Name: "Alice", Age: 30}
fmt.Println(numbers, fruits, colors, person)
}
Variables
Variables in Go are explicitly declared and typed. You can declare variables using var
or use shorthand notation with :=
for type inference.
Declaring Variables
package main
import "fmt"
func main() {
// Explicit type declaration
var name string = "John"
var age int = 25
// Type inference with var
var height = 175.5
// Shorthand declaration
country := "USA"
fmt.Println(name, age, height, country)
}
Zero Values
When variables are declared without an explicit initial value, they are given a zero value:
0
for numeric typesfalse
for booleans""
(empty string) for stringsnil
for pointers, functions, interfaces, slices, channels, and maps
int year;
year = 2024;
int month = 8;
float temperature = 23.5f;
double distance = 42.195;
char grade = 'B';
Control Structures
Go provides a range of control structures for managing the flow of execution in programs.
Conditional Statements
- if: Basic conditional branching.
- else if / else: Additional conditional branches.
package main
import "fmt"
func main() {
age := 18
if age < 18 {
fmt.Println("Minor")
} else if age == 18 {
fmt.Println("Just turned adult")
} else {
fmt.Println("Adult")
}
}
Switch Statements
Switch statements in Go are more flexible and can evaluate different types, including strings and integers.
package main
import "fmt"
func main() {
day := "Tuesday"
switch day {
case "Monday":
fmt.Println("Start of the work week")
case "Tuesday":
fmt.Println("Second day")
case "Wednesday":
fmt.Println("Midweek")
default:
fmt.Println("Other day")
}
}
Loops
- for: The only loop construct in Go, used for iteration.
package main
import "fmt"
func main() {
// Traditional for loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}
// For loop as a while loop
j := 0
for j < 5 {
fmt.Println(j)
j++
}
// Infinite loop
k := 0
for {
fmt.Println(k)
if k >= 5 {
break
}
k++
}
}
Range
The range
keyword is used to iterate over elements of an array, slice, map, or channel.
package main
import "fmt"
func main() {
fruits := []string{"apple", "banana", "cherry"}
for index, fruit := range fruits {
fmt.Printf("%d: %s\n", index, fruit)
}
}