Golang Program to print and declare variables

April 13, 2022, Learn eTutorial

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

What is variable in programming language?

Variable is a value that may change depending on certain condition or on information passed to the program.

Variables in Golang

Go lang variables can be defined as a piece of storage containing data temporarily to work with it.

Generally a variable in Go lang uses the keyword varexplicit type and an assignment. The variables are declared with the desired name and the type of value that it will hold.

How to display and declare different types of variables in the Go Program

We are using fmt println() function for printing the string to the output screen. Here we are showing how to print and declare different types of variables in the Go language. Here variables strs, strc is a string variable and intx, inty, intb is an integer variable and blna is a boolean variable. Given below are the steps which are used in the Go program. 


STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Initialise string variable s with value "first" and print using fmt.println()

STEP 4: Initialise integer variable intx, inty with values "5,7" and print using fmt.println()

STEP 5: Initialise boolean variable blna with value "true" and print using fmt.println()

STEP 6: Declare integer variable intb and print using fmt.println()

STEP 7: Initialise string variable strc with value "second" and print using fmt.println()

Golang Source Code

                                          package main
import "fmt"

func main() {
    var strs = "first"

    var intx, inty int = 5, 7
    fmt.Println(intx, inty)

    var blna = true

    var intb int

    strc:= "second"


5 7