Golang Basics | Find Output Programs | Set 3

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

Program 1:

package main

import "fmt"

func main() {
    let var1=10
    let var2=10
    var var3=0
    
    var3 = var1 + var2
    fmt.Println("Sum is: ",var3)
}

Output:

./prog.go:6:9: syntax error: unexpected var1 at end of statement
./prog.go:7:9: syntax error: unexpected var2 at end of statement

Explanation:

The above program will generate a syntax error because there is no keyword with the name 'let'.


Program 2:

package main

import "fmt"

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

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

Output:

Sum is:  20

Explanation:

In the above program, we created two integer constants var1, var2, and one variable var3. Then we calculated the addition and printed the result on the console screen.


Program 3:

package main

import "fmt"

func main() {
	const var1 = 10
	const var2 = 10
	const var3 = var1 + var2

	fmt.Println("Sum is: ", var3)
}

Output:

Sum is:  20

Explanation:

In the above program, we created three integer constants var1, var2, and var3. Then we the value of var3 on the console screen.


Program 4:

package main
import "fmt"

func main() {
    const var1=10
    const 2var=10
    const var3=var1+2var
    
    fmt.Println("Sum is: ",var3)
}

Output:

./prog.go:6:11: syntax error: unexpected literal 2, expecting name
./prog.go:6:11: missing value in const declaration
./prog.go:7:22: syntax error: unexpected var at end of statement
./prog.go:9:8: syntax error: unexpected ., expecting type

Explanation:

The above program will generate syntax errors because we cannot create an identifier starting with a digit. Here, 2var is an invalid identifier.


Program 5:

package main

import "fmt"

func main() {
	var num int = 10

	fmt.Print("Enter the value of num: ")
	fmt.scanf("%d", &num)

	fmt.Println("Value of num is: ", num)
}

Output:

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

Explanation:

The above program will generate syntax error, because we used fmt.scanf() instead of fmt.Scanf() to an integer value from the user.

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





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