This section contains the Golang structures find output programs (set 1) with their output and explanations.
Program 1:
package main
import "fmt"
type Student struct {
Id int
Name string
Fees int
}
func main() {
var stu Student{Id:101,Name:"John",Fees:12490}
fmt.Printf("Student Information:")
fmt.Printf("\n\tStudent Id : %d", stu.Id)
fmt.Printf("\n\tStudent Name : %s", stu.Name)
fmt.Printf("\n\tStudent Fees : %d", stu.Fees)
}
Program 2:
package main
import "fmt"
Student struct {
Id int
Name string
Fees int
}
func main() {
stu := Student{Id:101,Name:"John",Fees:12490}
fmt.Printf("Student Information:")
fmt.Printf("\n\tStudent Id : %d", stu.Id)
fmt.Printf("\n\tStudent Name : %s", stu.Name)
fmt.Printf("\n\tStudent Fees : %d", stu.Fees)
}
Program 3:
package main
import "fmt"
type Student struct {
Id int
Name string
Fees int
}
func main() {
stu := Student{101, "John", 12490}
fmt.Printf("Student Information:")
fmt.Printf("\n\tStudent Id : %d", stu.Id)
fmt.Printf("\n\tStudent Name : %s", stu.Name)
fmt.Printf("\n\tStudent Fees : %d", stu.Fees)
}
Program 4:
package main
import "fmt"
type Student struct {
Id int
Name string
Fees int
}
func main() {
stu := Student{Id: 101, Fees: 12490}
fmt.Printf("Student Information:")
fmt.Printf("\n\tStudent Id : %d", stu.Id)
fmt.Printf("\n\tStudent Name : %s", stu.Name)
fmt.Printf("\n\tStudent Fees : %d", stu.Fees)
}
Program 5:
package main
import "fmt"
type Student struct {
Id int
Name string
Fees int
}
func main() {
stu1 := Student{Id: 101, Fees: 12490}
stu2 := Student{Fees: 15470, Id: 102}
stu3 := Student{Name: "ABC"}
fmt.Printf("Student Information:")
fmt.Printf("\nStudent1:")
fmt.Printf("\n\tStudent Id : %d", stu1.Id)
fmt.Printf("\n\tStudent Name : %s", stu1.Name)
fmt.Printf("\n\tStudent Fees : %d", stu1.Fees)
fmt.Printf("\n\nStudent2:")
fmt.Printf("\n\tStudent Id : %d", stu2.Id)
fmt.Printf("\n\tStudent Name : %s", stu2.Name)
fmt.Printf("\n\tStudent Fees : %d", stu2.Fees)
fmt.Printf("\n\nStudent3:")
fmt.Printf("\n\tStudent Id : %d", stu3.Id)
fmt.Printf("\n\tStudent Name : %s", stu3.Name)
fmt.Printf("\n\tStudent Fees : %d", stu3.Fees)
}
Answer Program 1:
Output:
Explanation:
The above program will generate a syntax error because we did not initialize stu object properly. The correct way of initialization is given below,
var stu = Student{Id:101,Name:"John",Fees:12490}
Answer Program 2:
Output:
Explanation:
The above program will generate a syntax error because we did not declare Student properly. We need to use the type keyword to declare a structure in the Go language. The correct declaration is given below,
Answer Program 3:
Output:
Explanation:
In the above program, we created a structure Student that contains Id, Name, Fees members. Then we created an object of Student structure with initial values in the main() function. After that, we printed the student information.
Answer Program 4:
Output:
Explanation:
In the above program, we created a structure Student that contains Id, Name, Fees members. Then we created an object of Student structure with initial values for Id and Name in the main() function.
But here we did not assign the value for student name. That's why it was not initialized. After that, we printed the student information.
Answer Program 5:
Output:
Explanation:
In the above program, we created a structure Student that contains Id, Name, Fees members. Then we created the objects of the Student structure and initialized them partially in the main() function.
After that, we printed the student information.
need an explanation for this answer? contact us directly to get an explanation for this answer