sudo apt - get install golang
brew install go
After installation, open your terminal and run go version
. If it displays the installed Go version, the installation was successful.
Here is a simple “Hello, World!” program in Golang:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
package main
: Every Go program must belong to a package. main
is a special package that indicates an executable program.import "fmt"
: The fmt
package provides functions for formatted input and output.func main()
: The main
function is the entry point of an executable Go program.package main
import "fmt"
func main() {
// Declaration and initialization
var age int = 25
fmt.Println(age)
// Short variable declaration
name := "John"
fmt.Println(name)
}
int
, int8
, int16
, int32
, int64
, uint
, uint8
, uint16
, uint32
, uint64
, float32
, float64
bool
with values true
or false
package main
import "fmt"
func main() {
age := 20
if age >= 18 {
fmt.Println("You are an adult.")
} else {
fmt.Println("You are a minor.")
}
}
package main
import "fmt"
func main() {
for i := 0; i < 5; i++ {
fmt.Println(i)
}
}
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func main() {
result := add(3, 5)
fmt.Println(result)
}
package main
import "fmt"
func main() {
var num int = 10
var ptr *int = &num
fmt.Println("Value of num:", num)
fmt.Println("Address of num:", &num)
fmt.Println("Value of ptr:", ptr)
fmt.Println("Value at the address ptr points to:", *ptr)
}
package main
import "fmt"
type Person struct {
name string
age int
}
func main() {
p := Person{name: "Alice", age: 22}
fmt.Println(p.name, p.age)
}
package main
import "fmt"
func main() {
var numbers [5]int
numbers[0] = 1
numbers[1] = 2
fmt.Println(numbers)
}
package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5}
fmt.Println(slice)
}
package main
import "fmt"
func main() {
m := make(map[string]int)
m["apple"] = 10
m["banana"] = 20
fmt.Println(m)
}
package main
import (
"fmt"
"time"
)
func printNumbers() {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(i)
}
}
func main() {
go printNumbers()
time.Sleep(1000 * time.Millisecond)
fmt.Println("Main function ended.")
}
package main
import (
"errors"
"fmt"
)
func divide(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
func main() {
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
}
Use gofmt
to format your code automatically. It helps in maintaining a consistent code style.
Design your packages in a modular way. Each package should have a single, well - defined responsibility.
Always handle errors properly. Don’t ignore errors as they can lead to unexpected behavior.
When working with concurrent code, use synchronization mechanisms like mutexes to avoid race conditions.
In this tutorial, we have covered the fundamental concepts of Golang, including setting up the environment, basic syntax, variables, control structures, functions, pointers, structs, arrays, slices, maps, concurrency, and error handling. We also discussed common practices and best practices. By mastering these concepts, beginners can start building efficient and scalable applications using Golang.