Skip to content

Instantly share code, notes, and snippets.

@hygull
Created December 10, 2016 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hygull/04d14794bd58074490d2e31f2740480c to your computer and use it in GitHub Desktop.
Save hygull/04d14794bd58074490d2e31f2740480c to your computer and use it in GitHub Desktop.
Structure and its use in 3 ways created by hygull - https://repl.it/EmYJ/3
/*
{
"created_after" : "Sat Dec 10 07:27:03 IST 2016",
"focus_of_program" : "Structure and its usage in 3 diffferent ways"
"coded_by" : "Rishikesh Agrawani"
}
*/
package main
import "fmt"
func main() {
//Defining structure for storing the details of a Person
type Book struct{
Name string
Author string
Price float32
Pages int32
}
//1st way
var book1 Book
book1 = Book{"Go In Action","William Kennedy",430.50,320}
//2nd way (Direct way of creating Book object)
book2 := Book{"Playing With Golang", "Rishikesh Agrawani", 300.67,400}
//3rd way
var book3 Book = Book{"The Basics Of programming","Rob Pike",500.50,450}
//Slice of Book (i.e 3 books)
booksList :=[]Book{book1,book2,book3}
//Displaying the details of all the 3 books using for loop
for index,book := range booksList {
fmt.Println("Book",index+1,"'s details :-\n")
fmt.Println("Book's name :\t",book.Name)
fmt.Println("Book's author :\t",book.Author)
fmt.Println("Book's price :\t",book.Price)
fmt.Println("Number of pages :\t",book.Pages)
fmt.Println() //Newline
}
}
/*OUTPUT:-
Book 1 's details :-
Book's name : Go In Action
Book's author : William Kennedy
Book's price : 430.5
Number of pages : 320
Book 2 's details :-
Book's name : Playing With Golang
Book's author : Rishikesh Agrawani
Book's price : 300.67
Number of pages : 400
Book 3 's details :-
Book's name : The Basics Of programming
Book's author : Rob Pike
Book's price : 500.5
Number of pages : 450
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment