This page looks best with JavaScript enabled

Let's Learn Go -- (2) Data and Logic Structures

 ·  ☕ 3 min read

1. Go’s Data Structures

Basic types

  • Boolean: bool
  • Integer: byte, int, int8, int16, uint, uintptr
  • Floating point: float32, float64
  • Complex: complex64, complex128
  • String: string
  • Character: rune
  • Error: error

Composite types

  • Pointer: pointer
  • Array: array
  • Slice: slice
  • Dictionary: map
  • Channel: chan
  • Struct: struct
  • Interface: interface

A Go variable identifier is made up of letters, digits, and underscores, and its first character cannot be a digit. When declaring a variable, the type goes after the variable identifier. The general form is:

1
2
3
4
5
// a single variable
var identifier type

// multiple variables
var identifier1, identifier2 type

When declaring a variable, if it is not initialized it defaults to the zero value. For a variable declaration with an initial value, the type can be omitted, because Go can infer the type.

1
2
3
4
var i int = 0
var i = 0
// You can also omit the var keyword and declare the variable with the := syntactic sugar
i := 0

2. Go’s Logic Structures

  • Loop structure for
1
2
3
for i := 0; i < 10; i++ {
    sum += i
}
1
2
3
4
sum := 0
for sum < 1000 {
    sum += sum
}
  • Conditional branches if, switch
1
2
3
4
5
if x < 0 {
    return -x
} else {
    return x
}
1
2
3
4
5
6
7
8
switch t := i.(type) {
case bool:
    return "I'm a bool"
case int:
    return "I'm an int"
default:
    return "Don't know type %T", t
}
  • Jump goto

goto can jump unconditionally to a specified label

1
2
3
4
5
6
if error {
  goto doError
}
// the code block skipped when an error occurs
doError:
// the code block executed immediately when an error occurs
  • Defer defer

A defer statement is pushed onto a stack, and when the outer function returns, the statements are popped off and executed in order.

1
2
3
4
func main() {
    defer second_run()
    first_run()
}

3. Go’s Module Definitions

3.1 Functions

The format for defining a Go function:

1
2
3
func function_name( [parameter list] ) [return_types] {
    // function body
}
  • func, the function declaration keyword
  • function_name, the function name, which together with the parameter list forms the function signature
  • parameter list, the parameter list
  • return_types, the return type

An example:

1
2
3
4
5
6
7
func max(num1, num2 int) int {
    if (num1 > num2) {
      return num1
    } else {
      return num2
    }
}

A Go function can also return multiple values:

1
2
3
func addAndMult(a int, b int) (int, int){
    return a + b, a * b
}

In addition, Go also lets you declare an anonymous function and assign it directly to a variable:

1
2
3
my_func_name := func() {
    // function body
}

3.2 Packages

Go uses package to manage code, similar to Python. A package is a collection of one or more Go source files. Go has many built-in packages, such as fmt, os, io, and so on.

To declare a package, add the following identifier on the first line of the file:

1
package package_name

To import it, add the following identifier at the top of the file:

1
import "path/package_name"

Features of package:

  • Sibling files in the same directory belong to one package
  • The package name and the directory name can differ, but they are usually kept consistent
  • A program’s entry point is the main function of the main package; if there is no main package, no executable file is generated
  • Only identifiers whose first letter is uppercase are accessible outside the package

微信公众号
WRITTEN BY
微信公众号