CS 245 - Programming Languages

Lab 3

Little Go Programs

In this lab you will write, and or analyze a series of small programs in Go. The intent here is simply to get you some practice with Go before starting on the first programming assignment.

Fizz Buzz

Write a program that prints the numbers 1 .. 100 except: if the number is divisible by 3 print "Fizz", if the number is divisible by 5 print "Buzz", if the number is divisible by 3 and 5 print "FizzBuzz".You should have at least one function other than main.

K to M conversion

Write a program that takes input from the command line to convert miles to kilometers or kilometers to miles. (1 mile equals 1.609km). For instance, if your command line was
        go run convert.go 10 k
    
then the output would be "6.14 miles". Conversely, if your command line was
        go run convert.go 5 m
    
then the output should be "8.05 k" For this program you will need to use the ParseFloat function in the strconv package. You should look up how to do this.

Getting command line input is easy in Go, if different from Java. See this program for an example. In addition you will need to convert the strings you received from the command line into numbers. See this program for an example.

Student Structs

Write a new Go program that does the following:
  1. Create a struct that holds a date (month, day and year).
  2. Create a String() method for this struct that results in a nice format for the date. (See the String method in here for an example.
  3. Create a struct that holds information about a student: specifically: name, id and birth date. You should use the Date struct in your student struct.
  4. Initialize several instances of the student struct. Use only one statement to initialize
  5. Print these instances

Mapped students

Take your student structs from the previous problem and put them into a map that is indexed by the student id. When done, simply print the map. (Maps in Go are equivalent to Hashtables/Hashmaps in Java. You will need to research for yourself how to create and use Go maps.)

What to turn in

When you are done send email to gtowell@brynmawr.edu with each of the Go programs you wrote. As usual for labs, you are only expected to spend 80 minutes on this; so send what you have at the end of that time.