Welcome To Golang By Example

Constant array or slice in Go (Golang)

Table of Contents

Overview

Go only supports four types of constant

Go doesn’t support const array or slice. It is because in go constant value is computed at compile time. Arrays or slices are always evaluated at run time. So below program would raise a compilation error

Example

package main
func main() {
	const e = [1]int{1}
}

Output

const initializer [1]int literal is not a constant