Learning Golang: Assigning and printing variables

Share on:

This is part 14 of my journey learning Golang.

As an exercise for Codecademy’s Learn Go course, I wrote my second Go program.

It simulates a catalog for a comic book store.

At this point the course has only covered the most fundamental parts of Go, like variables, simple data types, basic arithmetic, and printing. Arrays and functions are still out of the picture.

For that reason, the structure of the program is very primitive and there is a lot of repetition (room for improvement).

That still made it a valuable exercise. Some of the concepts that this little program reinforces are:

  • Declaring variables of different data types.
  • Assigning (and re-assigning) values to variables.
  • Doing basic arithmetic.
  • Mixing whole numbers arithmetic with fractional numbers arithmetic (data type conversion).
  • Printing out variable values along with text literals.

This is the full code:

 1package main
 2
 3import(
 4  "fmt"
 5)
 6
 7func main() {
 8  // Comic book variables
 9  var publisher, writer, artist, title, genre string
10  var year, pageNumber, age int
11  var grade, price float32
12
13  // Define first comic book
14  title = "Mr. GoToSleep"
15  genre = "Mistery"
16  writer = "Tracey Hatchet"
17  artist = "Jewel Tampson"
18  publisher = "DizzyBooks Publishing Inc."
19  year = 1997
20  pageNumber = 14
21  grade = 6.5
22  age = 2020 - year
23  price = float32(age * pageNumber) * grade / 100.0
24
25  // Print out variables
26  fmt.Println(
27    title,
28    "genre", genre,
29    "written by", writer,
30    "drawn by", artist,
31    "published by", publisher,
32    "on", year,
33    "containing", pageNumber, "pages",
34    "in condition", grade,
35    "price", price,
36  )
37
38  // Define second comic book
39  title = "Epic Vol. 1"
40  genre = "SciFi"
41  writer = "Ryan N. Shawn"
42  artist = "Phoebe Paperclips"
43  publisher = "DizzyBooks Publishing Inc."
44  year = 2013
45  pageNumber = 160
46  grade = 9.0
47  age = 2020 - year
48  price = float32(age * pageNumber) * grade / 100.0
49
50  // Print out variables
51  fmt.Println(
52    title,
53    "genre", genre,
54    "written by", writer,
55    "drawn by", artist,
56    "published by", publisher,
57    "on", year,
58    "containing", pageNumber, "pages",
59    "in condition", grade,
60    "price", price,
61  )
62
63  // Define third comic book
64  title = "Ms. Y"
65  genre = "Adventure"
66  writer = "Gordon Ryan"
67  artist = "Isobelle Leclair"
68  publisher = "Astra Books"
69  year = 2001
70  pageNumber = 47
71  grade = 8.5
72  age = 2020 - year
73  price = float32(age * pageNumber) * grade / 100.0
74
75  // Print out variables
76  fmt.Println(
77    title,
78    "genre", genre,
79    "written by", writer,
80    "drawn by", artist,
81    "published by", publisher,
82    "on", year,
83    "containing", pageNumber, "pages",
84    "in condition", grade,
85    "price", price,
86  )
87
88}

Source: learning-go/comic-mischief/main.go

The program prints this out:

1Mr. GoToSleep genre Mistery written by Tracey Hatchet drawn by Jewel Tampson published by DizzyBooks Publishing Inc. on 1997 containing 14 pages in condition 6.5 price 20.93
2Epic Vol. 1 genre SciFi written by Ryan N. Shawn drawn by Phoebe Paperclips published by DizzyBooks Publishing Inc. on 2013 containing 160 pages in condition 9 price 100.8
3Ms. Y genre Adventure written by Gordon Ryan drawn by Isobelle Leclair published by Astra Books on 2001 containing 47 pages in condition 8.5 price 75.905

comments powered by Disqus