Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Go Constants

by suresh

Constants in Go programming are the variable whose values remain fixed. Once we declared a constant variable and assigned a value, we later cannot change its value. In Golang Programming, we can declare a constant variable using the const keyword. For instance, 

const info string = "Welcome to Go Tutorial"

If you observe the constant declaration, it is the same as the variable declaration. We replaced the var keyword with const. We can declare a constant without a data type.

const num = 200

It is a simple golang constant string example. Here, we declared a constant string using const and assigned a welcome message.

package main

import "fmt"

func main()  {
    const msg string = "Welcome to Go Tutorial"
    fmt.Println(msg)
}
Go Constant 1

Go Constant Example

In this example, we are trying to assign the new message to the msg constant variable. As you can see, golang const program is throwing an error.

package main

import "fmt"

func main()  {
    const msg string = "Welcome to Go Tutorial"
    fmt.Println(msg)

    msg = "New Message"
    fmt.Println(msg)
}
Go Constant 2

Golang Multiple Constants Declaration

We can use open and closed brackets to group or declare multiple constants. In this go example, we listed three constant variables and then performed some arithmetic operations on them.

package main

import "fmt"

func main()  {
    const (
        x = "Hi"
        y = "Hello"
        z = 2
    )
    fmt.Println(x)
    fmt.Println(y)
    fmt.Println(z)
    fmt.Println(x + y)
    fmt.Println(z * 2)
}
Golang Constant 3

Placed Under: Go Tutorial

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy