×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Basics | Find Output Programs | Set 2

This section contains the Golang basics find output programs (set 2) with their output and explanations.
Submitted by Nidhi, on August 06, 2021

Program 1:

package main

import "fmt"

func main() {
	var var1 int
	var1, _ = fmt.Printf("Hello World")
	fmt.Println(var1)
}

Output:

Hello World11

Explanation:

In the above program, we created an integer variable var1. The fmt.Printf() function returns the total number of printed characters. Then we assigned the result of fmt.Printf() to the var1 which is the total number of printed characters.


Program 2:

package main

import "fmt"

func main() {
	var var1 int
	var1, _ = fmt.Println("Hello World")
	fmt.Println(var1)
}

Output:

Hello World
12

Explanation:

Function fmt.Println() returns the total number of printed characters. Thus, the statement fmt.Println("Hello World") will print "Hello World" and a new line character, so the total number of printed characters will be 12.


Program 3:

package main

import "fmt"

func main() {
	var var1 int
	var1, _ = fmt.Print("Hello World")
	fmt.Println(var1)
}

Output:

Hello World11

Explanation:

Function fmt.Print() returns the total number of printed characters. Thus, the statement fmt.Print("Hello World") will print "Hello World" without a new line character, so the total number of printed characters will be 11.


Program 4:

package main

import "fmt"

func main() {
	var var1 string
	fmt.Print(var1)
}

Output:


Explanation:

Nothing will print because string var1 is declared but not initialized with any string.


Program 5:

package main

import "fmt"

func main() {
	var var1 = 10
	var var2 = 10
	var var3 = 0

	var3 = var1 + var2
	fmt.Println("Sum is: ", var3)
}

Output:

Sum is:  20

Explanation:

In the above program, we created three variables without specifying the data types. Golang auto detects the type of the variables based on initialized value. Read more about the variable declarations in Go.

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.