×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Basics | Find Output Programs | Set 5

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

Program 1:

package main
import "fmt"

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

Output:

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

Explanation:

The above program will generate a syntax error. Because we did not declare variables properly. The correct way to declare variables is given below,

var var1 int = 10
var var2 int = 10
var var3 int = 0

Program 2:

package main

import "fmt"

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

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

Output:

./prog.go:6:11: undefined: float
./prog.go:7:11: undefined: float
./prog.go:8:11: undefined: float

Explanation:

The above program will generate a syntax error. Because, we float is not a built-in data type in Golang. The correct way to declare variables is given below,

var var1 float32 = 10
var var2 float32 = 10
var var3 float32 = 0

OR

var var1 float64 = 10
var var2 float64 = 10
var var3 float64 = 0

Program 3:

package main

import "fmt"

func main() {
	var var1 float32 = 10.35
	var var2 float64 = 10.35

	if var1 == var2 {
		fmt.Println("Hello")
	} else {
		fmt.Println("Hiiii")
	}
}

Output:

./prog.go:9:10: invalid operation: var1 == var2 (mismatched types float32 and float64)

Explanation:

The above program will generate a syntax error. Because, we cannot compare variables of float32 and float64 data types using the "==" operator. We need to use variables of the same type.


Program 4:

package main

import "fmt"

func main() {
	var var1 float32 = 10.35

	if var1 == 10.35 {
		fmt.Println("Hello")
	} else {
		fmt.Println("Hiiii")
	}
}

Output:

Hello

Explanation:

In the above program, we created a variable var1 of float32 type initialized with 10.35. Then we compared variable var1 with the value 10.35 and printed a "Hello".


Program 5:

package main

import "fmt"

func main() {
	var var1 boolean = true

	if var1 == true {
		fmt.Println("Hello")
	} else {
		fmt.Println("Hiiii")
	}
}

Output:

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

Explanation:

In the above program, we created a variable var1 of boolean type. But boolean is not a valid type. Here, we need to use bool instead of boolean.

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





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