Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

Multiple constant declarations in Go (Golang)

Posted on September 30, 2020September 30, 2020 by admin

Table of Contents

  • Overview
  • Declaring multiple const together with different value and type
  • Declaring multiple const together with same value and type
  • Combining above two
  • Multiple declaration in single line

Overview

Below are some of the ways of declaring multiple constant together

Declaring multiple const together with different value and type

const (
  a = "circle"
  b = 1
  c float = 4.65
)

The declaration can be typed or untyped. Refer to this article to understand the difference between typed and untyped constant – https://golangbyexample.com/typed-untyped-constant-golang/

  • a is a untyped declaration. It will be of string type with value “circle”
  • b is also a untyped declaration. It will be of int type with value 1
  • c is a typed declaration. It will be of float64 type with value as 4.65

Declaring multiple const together with same value and type

const (
  a string = "circle"
  b
)

When constant type and value is not provided, then it gets its type and value from previous declaration

  • a will be of string type with value “circle”
  • b will be of type string and value will be “circle”

Combining above two

const (
  a string "circle"
  b
  c = 1
)
  • a will be of string type with value “circle”
  • b will be of type string and value will be “circle”
  • c will be of int type with value 1

Multiple declaration in single line

const a, b = 1, 2
const c, d int = 3, 4

Declaration again can be typed or untyped

  • a will be of int type with value 1
  • b will be of int type with value 2
  • c will be of int type with value 3
  • d will be of int type with value 4
  • go
  • golang
  • Follow @golangbyexample

    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

    ©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme