This section contains the Golang basics find output programs (set 2) with their output and explanations.
Program 1:
package main
import "fmt"
func main() {
var var1 int
var1, _ = fmt.Printf("Hello World")
fmt.Println(var1)
}
Program 2:
package main
import "fmt"
func main() {
var var1 int
var1, _ = fmt.Println("Hello World")
fmt.Println(var1)
}
Program 3:
package main
import "fmt"
func main() {
var var1 int
var1, _ = fmt.Print("Hello World")
fmt.Println(var1)
}
Program 4:
package main
import "fmt"
func main() {
var var1 string
fmt.Print(var1)
}
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)
}
Answer Program 1:
Output:
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.
Answer Program 2:
Output:
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.
Answer Program 3:
Output:
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.
Answer Program 4:
Output:
Explanation:
Nothing will print because string var1 is declared but not initialized with any string.
Answer Program 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer