Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. /*
  2.  
  3. {
  4. "created_at" : "Sat Dec 10 07:27:03 IST 2016",
  5. "aim_of_program" : "Structure"
  6. "coded_by" : "Rishikesh Agrawani"
  7. }
  8.  
  9. */
  10. package main
  11. import "fmt"
  12.  
  13. func main() {
  14.  
  15. //Defining structure for storing the details of a Person
  16. type Book struct{
  17. Name string
  18. Author string
  19. Price float32
  20. Pages int32
  21. }
  22.  
  23. //1st way
  24. var book1 Book
  25. book1 = Book{"Go In Action","William Kennedy",430.50,320}
  26.  
  27. //2nd way (Direct way of creating Book object)
  28. book2 := Book{"Playing With Golang", "Rishikesh Agrawani", 300.67,400}
  29.  
  30. //3rd way
  31. var book3 Book = Book{"The Basics Of programming","Rob Pike",500.50,450}
  32.  
  33. //Slice of Book (i.e 3 books)
  34. booksList :=[]Book{book1,book2,book3}
  35.  
  36. //Displaying the details of all the 3 books using for loop
  37. for index,book := range booksList {
  38. fmt.Println("Book",index+1,"'s details :-\n")
  39. fmt.Println("Book's name :\t",book.Name)
  40. fmt.Println("Book's author :\t",book.Author)
  41. fmt.Println("Book's price :\t",book.Price)
  42. fmt.Println("Number of pages :\t",book.Pages)
  43.  
  44. fmt.Println() //Newline
  45. }
  46. }
  47.  
  48. /*OUTPUT:-
  49.  
  50. Book 1 's details :-
  51.  
  52. Book's name : Go In Action
  53. Book's author : William Kennedy
  54. Book's price : 430.5
  55. Number of pages : 320
  56.  
  57. Book 2 's details :-
  58.  
  59. Book's name : Playing With Golang
  60. Book's author : Rishikesh Agrawani
  61. Book's price : 300.67
  62. Number of pages : 400
  63.  
  64. Book 3 's details :-
  65.  
  66. Book's name : The Basics Of programming
  67. Book's author : Rob Pike
  68. Book's price : 500.5
  69. Number of pages : 450
  70.  
  71. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement