×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Basics | Find Output Programs | Set 1

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

Program 1:

package main

import "fmt"

func main() {
	var var1 string
	var1 = "Hello World"

	fmt.Print(var1)
	fmt.Println(var1)
	fmt.Printf(var1)
}

Output:

Hello WorldHello World
Hello World

Explanation:

In the above program, we used fmt.Print(), fmt.Println(), and fmt.Printf() functions to print the value of var1 variable.


Program 2:

package main

import "fmt"

func main() {
	var var1 int
	var1 = 10

	fmt.printf("Value is: %d", var1)
}

Output:

./prog.go:9:2: cannot refer to unexported name fmt.printf

Explanation:

There is no such printf() function in fmt package, the correct function is Printf().


Program 3:

package main

import "fmt"

func main() {
	var var1 int
	var1 = 10

	fmt.Printf("Value is: %d")
}

Output:

./prog.go:6:6: var1 declared but not used

Explanation:

The above program will generate a syntax error because we created the variable var1 but it was not used.


Program 4:

package main

import "fmt"

func main() {
	var var1 Int
	var1 = 10

	fmt.Printf("Value is: %d", var1)
}

Output:

./prog.go:6:11: undefined: Int

Explanation:

While declaring the variable var1, we used data type "Int" which is not defined in Golang. The correct data type for integer is "int".


Program 5:

package main

import "fmt"

func main() {
	var var1 int

	var1 = fmt.Printf("Hello World")
	fmt.println(var1)
}

Output:

./prog.go:8:7: assignment mismatch: 1 variable but fmt.Printf returns 2 values
./prog.go:9:2: cannot refer to unexported name fmt.println

Explanation:

The above program will generate a syntax error, because the function fmt.Printf() returns two values but we used only one value as an lvalue.

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





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