Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Constant array or slice in Go (Golang)

Posted on September 30, 2023September 30, 2023 by admin

Table of Contents

  • Overview
  • Example

Overview

Go only supports four types of constant

  • Numeric ( int, int64, float, float64, complex128 etc)
  • String
  • Character or rune
  • Boolean

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

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP