Go is an open source programming language developed by Google and designed as a fast, efficient and reliable language. It combines the advantages of languages such as C and Python, making it a very popular programming language.
In this article, we will introduce the basics of the Go programming language.
Install Go
Before you start writing Go code, you need to install the Go programming language on your computer. You can download the installer for your operating system from the official website (https://golang.org/dl/). Just follow the instructions of the installer.
Write Hello World programs
Let's start with the Hello World program. Open your text editor and enter the following:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Save the file and name it hello.go. In the terminal window, change to the directory that contains the file and run the following command:
go run hello.go
The output should be:
Hello, World!
Variables and data types
Variable is one of the most basic building blocks in Go programs. A variable is an identifier used to store a value. In Go, variables must be declared before they can be used. The syntax for declaring variables is as follows:
var variable_name data_type
Here are some common data types:
Bool: Boolean, true or false
String: string, such as "hello world"
int: integer, such as 42
Float64: floating-point number, such as 3.14
The following is a sample program that demonstrates how to declare and use variables:
package main
import "fmt"
func main() {
var age int = 28
var name string = "Alice"
var isStudent bool = true
var height float64 = 1.65
fmt.Println(name, "is", age, "years old,", height, "meters tall, and is a student:", isStudent)
}
Alice is 28 years old, 1.65 meters tall, and is a student: true
Control flow
When writing a program, control flow statements allow us to execute different blocks of code according to specific conditions. Here are some commonly used control flow statements:
If/else statement: allows us to execute different blocks of code based on a condition.
for loop: allows us to execute the code block repeatedly until the specified condition is reached.
switch statement: allows us to execute different blocks of code according to different conditions.
The following is a sample program that demonstrates how to use the f / else statement and for loop:
package main
import "fmt"
func main() {
age := 28
if age >= 18 {
fmt.Println("You are an adult")
} else {
fmt.Println("You are a child")
}
for i := 1; i <= 10; i++ {
fmt.Println(i)
}
}
You are an adult
1
2
3
4
5
6
7
8
9
10
Function In Go
a function is a reusable block of code that takes some input parameters and returns a result. The following is a sample program that demonstrates how to declare and call a function:
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func main() {
sum := add(2, 3)
fmt.Println(sum)
}
5
Structure
A structure is a custom data type that can store different types of data. The following is a sample program that demonstrates how to declare and use structures:
package main
import "fmt"
type Person struct {
name string
age int
}
func main() {
person := Person{name: "Alice", age: 28}
fmt.Println(person.name, "is", person.age, "years old")
}
Alice is 28 years old
Package
packages are a way to organize related code. Every Go program must have a package called main, which is the entry point of the program. In addition, you can create your own packages and use them in other programs. The following is a sample program that demonstrates how to create and use packages:
Under the mathutils directory, create a file called add.go that contains the following:
package mathutils
func Add(a int, b int) int {
return a + b
}
In 'main. In the go °file, import the ^ mathutils package and call the functions in it:
package main
import (
"fmt"
"mathutils"
)
func main() {
sum := mathutils.Add(2, 3)
fmt.Println(sum)
}
5
The above is a short introductory tutorial on the Go programming language. If you want to learn more about the Go programming language, check out the official Go documentation (https:l/golang). Orglclocl).