Go is a programming language that is designed for simplicity, efficiency, and concurrency. Here’s a quick overview of its unique features and some code blocks for common tasks.
Declare variables using the var
keyword. Go is a statically typed language, but it can infer the data type of a variable from its value.
x := 10
c := 'a'
f := 3.14
Functions in Go are declared using the func
keyword. They can take parameters and return values.
func add(a int, b int) int {
return a + b
}
Go supports for
and while
loops for iterating over arrays or performing a task a certain number of times.
numbers := []int{1, 2, 3, 4, 5}
for _, number := range numbers {
fmt.Println(number)
}
i := 0
for i < len(numbers) {
fmt.Println(numbers[i])
i++
}
Use if
statements to execute code based on a condition. else if
and else
statements can be used to handle multiple conditions.
x := 10
if x > 0 {
fmt.Println("x is positive")
} else if x < 0 {
fmt.Println("x is negative")
} else {
fmt.Println("x is zero")
}
Go provides built-in support for file manipulation, including reading and writing files, and interacting with the file system.
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
data, err := ioutil.ReadFile("/path/to/file.txt")
if err != nil {
fmt.Println("Error reading file")
return
}
fmt.Println(string(data))
err = ioutil.WriteFile("/path/to/file.txt", []byte("New contents"), 0644)
if err != nil {
fmt.Println("Error writing file")
return
}
err = os.Remove("/path/to/file.txt")
if err != nil {
fmt.Println("Error deleting file")
return
}
fmt.Println("File deleted successfully")
}
Here are some resources to help you learn more about Go: