This section contains the Golang const, Type Casting find output programs (set 1) with their output and explanations.
Program 1:
package main
import "fmt"
func main() {
const num1 = 100
const num2 = 200
var sum = 0
sum = num1 + num2
fmt.Println("Sum: ", sum)
}
Program 2:
package main
import "fmt"
var num int=100;
const func Fun(){
num = 200;
}
func main() {
fmt.Println("Num: ",num);
Fun();
fmt.Println("Num: ",num);
}
Program 3:
package main
import "fmt"
func main() {
const arr = [5]int{1, 2, 3, 4, 5}
for i := 0; i < 5; i++ {
fmt.Println(arr[i])
}
}
Program 4
package main
import "fmt"
type Student struct {
const id int=101;
age int ;
name string;
}
func main() {
stu := Student{name: "Amit", age: 28 }
fmt.Println(stu);
}
Program 5:
package main
import "fmt"
func main() {
var num int = 100
const ptr *int = &num
fmt.Println(*ptr)
*ptr = 200
fmt.Println(num)
}
Answer Program 1:
Output:
Explanation:
In the above program, we created two constants num1 and num2 which was initialized with 100, 200 respectively. Then we added the values of num1 and num2 and assigned the result to the sum variable. After that, we printed the result on the console screen.
Answer Program 2:
Output:
Explanation:
The above program will generate syntax errors because we cannot create a const function in the Go language.
Answer Program 3:
Output:
Explanation:
The above program will generate a syntax error because we cannot create an array using the const keyword in the Go language.
Answer Program 4:
Output:
Explanation:
The above program will generate a syntax error because we cannot create the constant field of a structure.
Answer Program 5:
Output:
Explanation:
The above program will generate a syntax error because we cannot create a constant pointer like this in Go language.
need an explanation for this answer? contact us directly to get an explanation for this answer