Learn Go from Scratch
Hello! Welcome
Today we are going to learn about Go (Golang) from Scratch
What is Go?
Go is a programming language developed by Google, designed to be fast, safe, and simple.
With Go, you can create applications for the web, servers, and much more!
Why use Go?
Go is ideal for applications that require concurrency and high performance in large-scale networks and systems.
Additionally, its simplicity and compilation speed make Go perfect for server development and distributed applications.
You write your code in Go using a text editor, compile it, and Go turns it into a program that you can run.
You’re doing great!
Now let’s talk about the syntax of Go
Variables in Go
Variables in Go store data that your program needs. You can create variables for numbers, texts, booleans…
var x int = 5
y := 10 // quick declaration and assignment
sum := 5 + 10
With them, you can perform arithmetic, logical, and comparison operations.
Conditionals
With conditionals, you can make the program make decisions. We use if
and else
to execute code only if a condition is true.
For example:
if x > 5 {
fmt.Println("It is greater than 5")
}
Loops
Loops allow us to repeat actions. We use for
to repeat actions.
For example:
for i := 0; i < 5; i++ {
fmt.Println(i)
}
Functions in Go
Functions are blocks of code that perform specific tasks.
func sum(a int, b int) int {
return a + b
}
You can use functions to break your code into more manageable parts.
Arrays and Collections
Collections are structures that allow you to store multiple values. The simplest is an array.
var arr [5]int
slice := []int{1, 2, 3}
We also have slices, which are more flexible because they can change size dynamically.
Structures in Go
Structures are a way to group related data. With them, you can create custom data types for your program.
type Person struct {
Name string
Age int
}
They are useful for modeling entities with multiple properties.
Error Handling
Go has a simple approach to error handling, which helps you find and fix problems in your code.
result, err := functionThatCanFail()
if err != nil {
// handle the error
}
Concurrency in Go
Go makes concurrency easy, with goroutines
and channels
, ideal for simultaneous tasks.
go function()
Each goroutine
runs in parallel, optimizing performance.
You’re almost there!
We just need to see how to create a project in Go.
Create a Project
A good first project in Go is to write a program that displays a message on the screen.
- Create a folder for your project.
- Run
go mod init project_name
to initialize the module.
This sets up the project and manages dependencies.
Write Your First Program
Create a file main.go
and write your first program:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Compile and Run
Once your code is ready, compile it with Go and run the program to see the result.
go run main.go
Go will execute your code immediately.
Well done!
Now you know the basics of Go! Keep exploring and creating with this amazing language!