Golang Structures | Find Output Programs | Set 1

This section contains the Golang structures find output programs (set 1) with their output and explanations.
Submitted by Nidhi, on October 11, 2021

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)
}

Output:

./prog.go:11:17: syntax error: unexpected { at end of statement
./prog.go:13:2: syntax error: non-declaration statement outside function body

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}

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)
}

Output:

./prog.go:4:1: syntax error: non-declaration statement outside function body

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,

type Student struct {
	Id   int
	Name string
	Fees int
}

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)
}

Output:

Student Information:
	Student Id     : 101
	Student Name   : John
	Student Fees   : 12490

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.


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)
}

Output:

Student Information:
	Student Id     : 101
	Student Name   : 
	Student Fees   : 12490

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.


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)
}

Output:

Student Information:
Student1:
	Student Id     : 101
	Student Name   : 
	Student Fees   : 12490

Student2:
	Student Id     : 102
	Student Name   : 
	Student Fees   : 15470

Student3:
	Student Id     : 0
	Student Name   : ABC
	Student Fees   : 0

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.

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





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