![go programming language examples](https://yourselfhood.com/storage/2024/06/sunset-6492273_960_720.jpg)
Go, also known as Golang, is a powerful and efficient programming language that has gained popularity in recent years. Developed by Google, Go is designed to be simple, reliable, and efficient, making it a great choice for building scalable and robust applications. If you are looking to boost your coding skills and learn a new programming language, Go is a great language to start with.
To help you get started with Go programming language, we have compiled a list of examples that will guide you through the basics of the language and help you build your coding skills.
1. Hello World Program
Let’s start with the classic Hello World program. In Go, the syntax for printing to the console is simple and straightforward:
“`go
package main
import “fmt”
func main() {
fmt.Println(“Hello, World!”)
}
“`
2. Variables and Data Types
In Go, variables are declared using the `var` keyword followed by the variable name and type. Here is an example of declaring and initializing variables in Go:
“`go
package main
import “fmt”
func main() {
var name string = “John”
var age int = 30
var isMarried bool = false
fmt.Println(“Name:”, name)
fmt.Println(“Age:”, age)
fmt.Println(“Married:”, isMarried)
}
“`
3. Control Structures
Go supports various control structures like if-else statements, for loops, and switch cases. Here is an example of using if-else statement in Go:
“`go
package main
import “fmt”
func main() {
age := 30
if age >= 18 {
fmt.Println(“You are an adult”)
} else {
fmt.Println(“You are a minor”)
}
}
“`
4. Functions
Functions are a fundamental building block in Go programming. Here is an example of a simple function that takes two integers as input and returns their sum:
“`go
package main
import “fmt”
func add(a, b int) int {
return a + b
}
func main() {
result := add(10, 20)
fmt.Println(“Sum:”, result)
}
“`
5. Arrays and Slices
Arrays and slices are used to store collections of elements in Go. Here is an example of declaring and iterating over an array in Go:
“`go
package main
import “fmt”
func main() {
numbers := [5]int{1, 2, 3, 4, 5}
for i := 0; i