learn-go

Noted:

Trong Go, Chỉ có public, private

  • array - Nó là value type
  • slice - Nó là reference type tham chiếu tới mảng. Slice thường được tạo bằng cắt 1 phần của array. Mọi thay đổi trong slice sẽ ảnh hưởng đến mảng gốc và ngược lại.

Cli

go mod init
go get -u gopkg.in/yaml.v3
	// Chỉ muốn làm việc với value, bỏ qua index và ngược lại. 
	for _, value := range countries {
		fmt.Println(value)
	}
    for index, _ := range countries {
    fmt.Println(index)
    }

Syntax

  • array
# Declare 1
array_name := [length]Type{item1, item2, item3,...itemN}
a := [5]int{1, 2, 3, 4, 5}

# Declare 2
var a[5]int

# Declare 
  • slice
# Declare 1
[]T
or
[]T{}
or
[]T{value1, value2, value3, ...value n}

var my_slice[]int
var my_slice_1 = []string{"Geeks", "for", "Geeks"}

Keywords in Go

25 Keywords

break        default      func         interface    select
case         defer        go           map          struct
chan         else         goto         package      switch
const        fallthrough  if           range        type
continue     for          import       return       var

Let’s divide all 25 keywords in 4 groups.

Declaration

  • const

The const keyword is used to introduce a name for a scalar value like 2 or 3.14, etc.

package main

import "fmt"

const pi = 3.14

func main() {
    fmt.Println("Value of pi:", pi)
}
  • var

The var keyword is used to create the variables in the Go language.

package main

import "fmt"

func main() {
	var x int = 5
	fmt.Println("Value of x:", x)
}
  • func

The func keyword is used to declare a function.

package main

import "fmt"

func greet() {
    fmt.Println("Hello!")
}

func main() {
    greet()
}
  • type

We can use the type keyword to introduce a new struct type.

package main

import "fmt"

type Celsius float64

func main() {
	var temperature Celsius = 23.5
	fmt.Println("Temperature:", temperature, "Celsius")
}
  • import The import keyword is used to import packages.
  • package The code is grouped as a unit in a package. The package keyword is used to define one.

Composite Types

  • chan The chan keyword is used to define a channel. In Go, you are allowed to run parallel pieces of code simultaneously.
  • interface The interface keyword is used to specify a method set. A method set is a list of methods for a type.
  • map

The map keyword defines a map type. A map os an unordered collection of the key-value pairs.

package main

import "fmt"

func main() {
    studentGrades := map[string]int{
        "Alice": 90,
        "Bob":   85,
        "Charlie": 95,
    }

    fmt.Println("Bob's Grade:", studentGrades["Bob"])
}
  • struct

Struct is a collection of fields. We can use the struct keyword followed by the field declarations.

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    person := Person{"John Doe", 25}
    fmt.Println("Person:", person)
}

Control Flow

  • break The break keyword lets you terminate a loop and continue execution of the rest of the code.
  • case This is a form of a switch construct. We specify a variable after the switch.
  • continue The continue keyword allows you to go back to the beginning of the ‘for’ loop.
  • default The default statement is optional but you need to use either case or default within the switch statement. The switch jumps to the default value if the case does not match the controlling expression.
  • else The else keyword is used with the if keyword to execute an alternate statement if a certain condition is false.
  • fallthrough This keyword is used in a case within a switch statement. When we use this keyword, the next case is entered.
  • for

The for keyword is used to begin a for loop.

package main

import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        fmt.Println(i)
    }
}
  • goto The goto keyword offers a jump to a labeled statement without any condition.
  • if

The if statement is used to check a certain condition within a loop.

package main

import "fmt"

func main() {
    x := 10

    if x > 5 {
        fmt.Println("x is greater than 5")
    } else {
        fmt.Println("x is not greater than 5")
    }
}
  • range The range keyword lets you iterate items over the list items like a map or an array.
  • return Go allows you to use the return values as variables and you can use the return keyword for that purpose.
  • select The select keyword lets a goroutine to wait during the simultaneous communication operations.
  • switch

The switch statement is used to start a loop and use the if-else logic within the block.

package main

import "fmt"

func main() {
    day := "Saturday"

    switch day {
    case "Monday":
        fmt.Println("It's the start of the week.")
    case "Saturday", "Sunday":
        fmt.Println("It's the weekend!")
    default:
        fmt.Println("It's a regular weekday.")
    }
}

Function Modifier

  • defer The defer keyword is used to defer the execution of a function until the surrounding function executes.
  • go The go keyword triggers a goroutine which is managed by the golang-runtime.