Q:

Golang Basics | Find Output Programs | Set 1

belongs to collection: Golang Find Output Programs

0

This section contains the Golang basics find output programs (set 1) with their output and explanations.

Program 1:

package main

import "fmt"

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

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

 

Program 2:

package main

import "fmt"

func main() {
	var var1 int
	var1 = 10

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

 

Program 3:

package main

import "fmt"

func main() {
	var var1 int
	var1 = 10

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

 

Program 4:

package main

import "fmt"

func main() {
	var var1 Int
	var1 = 10

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

 

Program 5:

package main

import "fmt"

func main() {
	var var1 int

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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.


Answer Program 2:

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().


 

Answer Program 3:

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.


 

Answer Program 4:

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".


 

Answer Program 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang Basics | Find Output Programs | Set 2... >>