Before installing Go, make sure your system meets the following requirements:
.msi
file and follow the installation wizard..pkg
file and follow the prompts./usr/local
using the following command:sudo tar -C /usr/local -xzf go1.x.x.linux - amd64.tar.gz
PATH
variable. For example, in a Unix - like system, you can add the following line to your .bashrc
or .zshrc
file:export PATH=$PATH:/usr/local/go/bin
go version
If the installation is successful, it will display the installed Go version.
Here is a simple “Hello, World!” program in Go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
package main
: Every Go program must start with a package
declaration. A program that can be executed (not a library) must have the main
package.import "fmt"
: The import
statement is used to include external packages. The fmt
package provides functions for formatted input and output.func main()
: The main
function is the entry point of a Go program. It is where the program execution begins.To run the program, save the code in a file named hello.go
and run the following command in the terminal:
go run hello.go
In Go, you can declare variables in two ways:
package main
import "fmt"
func main() {
// Explicit declaration
var name string
name = "John"
// Short variable declaration
age := 25
fmt.Printf("Name: %s, Age: %d\n", name, age)
}
var name string
: This is an explicit variable declaration where you specify the variable name and its type.age := 25
: The short variable declaration syntax (:=
) is used to declare and initialize a variable in one step. The type is inferred from the assigned value.Go has several built - in data types, including:
int
, int8
, int16
, int32
, int64
, uint
, uint8
, uint16
, uint32
, uint64
float32
, float64
bool
string
package main
import "fmt"
func main() {
num := 10
if num > 5 {
fmt.Println("Number is greater than 5")
} else {
fmt.Println("Number is less than or equal to 5")
}
}
package main
import "fmt"
func main() {
// Traditional for loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}
// For - range loop for arrays
numbers := [3]int{1, 2, 3}
for index, value := range numbers {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}
}
Functions in Go are declared using the func
keyword:
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func main() {
result := add(3, 5)
fmt.Println("Sum:", result)
}
func add(a int, b int) int
: The function add
takes two integer parameters (a
and b
) and returns an integer.Go has a rich standard library, and you can also create your own packages. Here is an example of creating a simple package:
Create a directory named mathutils
and inside it, create a file named add.go
with the following code:
package mathutils
func Add(a int, b int) int {
return a + b
}
Create a new file named main.go
in the parent directory of mathutils
:
package main
import (
"fmt"
"./mathutils"
)
func main() {
result := mathutils.Add(2, 3)
fmt.Println("Sum:", result)
}
To build and run the program, use the following commands:
go build
./your_program_name
In Go, errors are just values. The error
type is a built - in interface in Go.
package main
import (
"fmt"
"strconv"
)
func main() {
numStr := "abc"
num, err := strconv.Atoi(numStr)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Number:", num)
}
}
The strconv.Atoi
function tries to convert a string to an integer. If the conversion fails, it returns an error.
One of the most powerful features of Go is its support for concurrency. Go uses goroutines and channels to achieve concurrent programming.
A goroutine is a lightweight thread of execution.
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")
}
go printNumbers()
: The go
keyword is used to start a new goroutine.Channels are used to communicate between goroutines.
package main
import "fmt"
func sendData(ch chan int) {
for i := 0; i < 5; i++ {
ch <- i
}
close(ch)
}
func main() {
ch := make(chan int)
go sendData(ch)
for num := range ch {
fmt.Println(num)
}
}
ch <- i
: This is the syntax for sending a value to a channel.num := range ch
: This is used to receive values from a channel until it is closed.gofmt
to format your code. It helps maintain a consistent code style across the project.In this quick - start tutorial, we have covered the fundamental concepts of Golang, including installation, basic syntax, variables, control structures, functions, packages, error handling, and concurrency. By following these concepts and best practices, you can start writing efficient and reliable Go programs. Go’s simplicity, performance, and built - in concurrency support make it a great choice for a wide range of applications, from web development to system programming.