golang built-in function list & operator list


For those who have somehow learned the syntax but can't remember the built-in functions. I will use it in my next job, so I will write it after studying. I'm sorry if I made a mistake.

A Tour of Go (Japanese) Go Programming Language Specification (Japanese)

Operator list

Most are common operators, but some are special.

Operator that becomes an expression

Binary operator

operator Priority Explanation operator Priority Explanation
x || y 1 Condition or(Short-circuit evaluation) x && y 2 Condition and(Short-circuit evaluation)
x == y 3 equal x != y 3 Not equal
x < y 3 Greater x > y 3 Smaller
x <= y 3 that's all x >= y 3 Less than
x + y 4 Sum / string concatenation x - y 4 difference
x | y 4 Bit or x ^ y 4 Bit xor
x * y 5 product x / y 5 quotient
x % y 5 Surplus
x << y 5 Bit left shift x >> y 5 Bit right shift
x & y 5 Bit and x &^ y 5 Bit clear(and not)

Prefix operator

operator Explanation operator Explanation
+x unary+ -x unary-
!x Logic not ^x Bit not
*x Indirect reference &x Address acquisition
<-ch Channel reception

Expressions using other symbols

operator Explanation operator Explanation
x[i] index x[low:high] slice
f(x) Function call x.f(y) Method call
T(x) Type conversion x.(T) Type assertion(as)

Operator that becomes a statement

Increment, decrement, and assignment are statements in Go, not expressions, so they cannot be used in expressions. You can use join operators such as + = -= for assignment.

operator Explanation operator Explanation
x++ Increment x-- Decrement
x = y Substitution ch <- value Channel transmission

Detailed explanation

& ^ (Bit clear)

x &^ y == x & (^y)

It's an operator that you don't often see, but it's not difficult and it behaves as it looks. Clears the bits of x whose y is 1 to 0.

<-(Channel operation)

value = <-ch //Receive(formula)
value, ok = <-ch //Receive(formula)OK if the channel is closed==false
ch <- value  //Send(Sentence)

It is an operator that operates the "channel" which is one of the features of Go. It behaves differently depending on the state of the channel. There are various explanations, but I think it is easy to understand if you think of "channel = asynchronous queue".

Channel state Send( ch <- val ) Receive( <-ch )
Sync channel(cap == 0) Block until you can send Block until received
Asynchronous channel(cap > 0) Add to queue Get from queue
Empty queue Add to queue Block until received
The queue is full Block until you can send Get from queue
Closed Runtime panic Get zero value instantly
nil channel Block forever Block forever


var array [5]int = [5]int{0, 1, 2, 3, 4}
var slice1 []int = array[2:4]  // {2, 3}
var slice2 []int = array[2:]   // {2, 3, 4}
var slice3 []int = array[:4]   // {0, 1, 2, 3}

Extracts an array/slice/string (considered as [] byte) whose index is low <= x <high. The return value will be a slice. If omitted, it is the same as specifying from end to end.

Index / type assertion

var myMap map[string]int
var x interface{}
var value int = myMap[key]
var value int = x.(int)

Index access to the map gets the value with a specific key. Type assertions assign a value to a particular type. If both fail, it will cause a runtime panic.

var value, ok = myMap[key]
var value, ok = x.(int)

These expressions (and channel receive expressions) can only return two return values ​​when used as the right-hand side of the assignment, and the second return value will return "successful" (no runtime panic). ).

List of built-in functions

Here's a function definition, just as you would when defining a regular function. T and K mean that they can be replaced with any type. <T> means to write the type itself as an argument.

Memory management system


func new(<T>) *T

Allocates memory of the required size for the T type, initializes it to zero, and returns a pointer to that value. Can be used with any type.


func make(<[]T>, len int)          []T     //Create slices of length len
         (<[]T>, len int, cap int) []T     //Create slices of length len, capacity cap
         (<map[K]T>)               map[K]T //Create an empty map
         (<map[K]T>, cap int)      map[K]T //Create an empty map of capacity cap
         (<chan T>)                chan T  //Create a sync channel
         (<chan T>, cap int)       chan T  //Create an async channel with a queue size of cap

A function that creates a built-in value. It takes length and capacity as arguments, depending on the type.

Collection channel operation system


func len(string)  int //String byte length
        ([n]T)    int //Array length(== n)
        (*[n]T)   int //Array length(== n)
        (map[K]T) int //Number of keys defined in the map
        ([]T)     int //Slice length
        (chan T)  int //Number of elements queued in the channel

Gets the number of elements that the collection actually stores.


func cap ([n]T)      int //Array length(== n)
         (*[n]T)     int //Array length(== n)
         (map[key]T) int //Map capacity
         ([]T)       int //Slice capacity
         (chan T)    int //Channel queue capacity

Gets the maximum number of elements that can be stored in the collection (= capacity, capacity).


func append(slice []T, tail ...T) []T

Generates a new slice with tail added to the end of slice. If cap (slice) <len (slice) + len (tail), then a slice that references the new array is returned. Otherwise, the contents of the array referenced by slice will be overwritten.


func copy(to []T, from []T) int

Copy the slice element from from to to. If the lengths are different, the shorter one will be adjusted. The return value is the number of copied elements.


func delete(m map[K]T, key K)

Removes a specific key from the map. If the key does not exist, no error will occur and nothing will happen.


func close(ch chan T)

Close the channel and mark it so that no more values ​​are sent.

Complex number system


func complex(re float32, im float32) complex64
            (re float64, im float64) complex128

Generates the corresponding complex type.


func real(complex64)  float32
         (complex128) float64

Gets the real part of a complex number.


func imag(complex64)  float32
         (complex128) float64

Gets the imaginary part of a complex number.

Runtime panic system


func panic(interface{})

Cause a runtime panic. It is an "exception occurrence" in other languages.


func recover() interface{}

Calling inside a deferred (defer) function will recover from a runtime panic. The value specified by the argument of panic is returned as the return value. It's an "exception catch" in other languages.


I started writing with some preparedness, but the number of built-in functions was not very large. This small amount may be the reason why it is said that "Go is simple and easy to learn".

Recommended Posts

golang built-in function list & operator list