Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Go Programming Tutorial

Introduction to Go

Go, also known as Golang, is an open-source programming language developed by Google. It is designed to be simple, efficient, and reliable, making it ideal for building large-scale, high-performance applications. Go is statically typed and compiled, which means you get all the benefits of static typing and the performance of compiled languages.

Installing Go

To start using Go, you need to install it on your machine. Follow the steps below to install Go:

1. Visit the official Go website: https://golang.org/dl/

2. Download the installer for your operating system.

3. Run the installer and follow the on-screen instructions.

4. Verify the installation by opening a terminal and typing:

go version
go version go1.17.1 darwin/amd64

Setting Up Your First Go Program

Now that you have Go installed, let's write a simple "Hello, World!" program.

1. Open your terminal.

2. Create a new directory for your Go project:

mkdir hello

3. Navigate to the directory:

cd hello

4. Create a new Go file named main.go:

touch main.go

5. Open main.go in your favorite text editor and add the following code:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

6. Save the file and go back to your terminal.

7. Run your Go program:

go run main.go
Hello, World!

Understanding Go Basics

Let's go over some basic concepts in Go:

Packages

Go programs are organized into packages. A package is a collection of Go files. The main package is special because it defines an executable program. Your Go code must start with a package declaration.

Imports

The import statement is used to include other packages into your program. In the example above, we imported the fmt package to use the Println function.

Functions

Functions in Go are defined using the func keyword. The main function is the entry point of a Go program.

Variables and Data Types

Declaring Variables

Variables in Go can be declared using the var keyword or using shorthand notation:

var name string = "John"
var age int = 30

// Shorthand notation
city := "New York"
temperature := 25.5

Basic Data Types

Go supports several basic data types:

  • Integer types: int8, int16, int32, int64, uint8, uint16, uint32, uint64, int, uint
  • Floating-point types: float32, float64
  • Boolean type: bool
  • String type: string

Control Structures

If Statements

The if statement is used for conditional execution:

if age > 18 {
    fmt.Println("Adult")
} else {
    fmt.Println("Not an adult")
}

For Loops

The for loop is the only loop construct in Go. It can be used in several forms:

// Basic for loop
for i := 0; i < 10; i++ {
    fmt.Println(i)
}

// While-like loop
i := 0
for i < 10 {
    fmt.Println(i)
    i++
}

// Infinite loop
for {
    fmt.Println("Infinite loop")
}

Functions

Defining Functions

Functions in Go are defined using the func keyword. Here is an example of a simple function:

func add(a int, b int) int {
    return a + b
}

Calling Functions

To call a function, use its name followed by parentheses containing any arguments:

result := add(3, 4)
fmt.Println(result) // Output: 7

Working with Arrays and Slices

Arrays

Arrays in Go are fixed-size, homogeneous collections of elements:

var arr [5]int
arr[0] = 1
fmt.Println(arr) // Output: [1 0 0 0 0]

Slices

Slices are more flexible and powerful than arrays. They are dynamically-sized and provide much of the functionality of arrays:

slice := []int{1, 2, 3, 4, 5}
fmt.Println(slice) // Output: [1 2 3 4 5]

slice = append(slice, 6)
fmt.Println(slice) // Output: [1 2 3 4 5 6]

Structs

Structs in Go are used to group together variables under a single name. They are similar to classes in other languages:

type Person struct {
    Name string
    Age  int
}

func main() {
    person := Person{Name: "John", Age: 30}
    fmt.Println(person) // Output: {John 30}
}

Concurrency

Go has built-in support for concurrent programming using goroutines and channels.

Goroutines

A goroutine is a lightweight thread managed by the Go runtime:

go func() {
    fmt.Println("Hello from a goroutine")
}()

Channels

Channels are used to communicate between goroutines:

ch := make(chan string)

go func() {
    ch <- "Hello, Channel"
}()

msg := <-ch
fmt.Println(msg) // Output: Hello, Channel

Conclusion

Congratulations! You have completed the basic Go programming tutorial. You should now have a solid understanding of Go's syntax and features. Keep practicing and explore more advanced topics to become proficient in Go programming. Happy coding!