go_language_guide
go_language_guide
1. Introduction to Go
3. Basic Syntax
Example:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Go has basic data types like int, float64, string, and bool.
Variables can be declared using 'var' or shorthand ':='.
Example:
var x int = 10
y := "Hello"
5. Control Structures
Example:
for i := 0; i < 5; i++ {
fmt.Println(i)
}
Example:
arr := [3]int{1, 2, 3}
slice := []int{1, 2, 3, 4}
m := map[string]int{"a": 1, "b": 2}
7. Functions
Example:
func add(a int, b int) int {
return a + b
}
Example:
type Person struct {
Name string
Age int
}
Example:
go func() {
fmt.Println("Running concurrently")
}()
Reading and writing files in Go is done using 'os' and 'io/ioutil' packages.
Example:
data, _ := ioutil.ReadFile("file.txt")
fmt.Println(string(data))
Example:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, Web!")
})
http.ListenAndServe(":8080", nil)