CS 245 - Programming Languages
Lab 3
Little Go Programs
In this lab you will write, and or analyse a series of small programs in Go. The intent here is simply to get you
some practice with Go before the first programming assignment.
When you are complete, send me answers for the questions in slice analysis and kilometer / Miles program. If you do
not complete both, send email with what you complete and a statement of where you go to.
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".
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". Comversely, 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.
Slice analysis
Look at this program. It has three functions that do much the same thing.
- Why do I comment that rrslice is less efficient than rslice?
- What is r3slice said to suck?
- How would you fix r3slice?
Manipulating slices
Take the go program for slice analysis and add a new function. (You might also want to remove the rrslice and
r3slice functions and their uses.) The function you write should take an integer slice and a
number as parameters. The function should return a subslice of the original slice such that the subslice has the
smallest sum of any
contiguous subslice of the original slice. For instance, given a slice containing [1,6,2,3,4] and a number 2, you
should return the subslice containing [2,3] as that is the portion of the original slice has the smallest sum.
Student Structs
- Create a struct that holds a date (month, day and year).
- Create a String() method for this struct that results in a nice format for the date.
- Create a struct that holds information about a student: specifically: name, id and birth date. You
should use the Date struct.
- Initialize several instances of the student struct. Use only one statement to initialize
- 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.