Skip to content

Instantly share code, notes, and snippets.

@jboursiquot
Last active December 5, 2018 13:57
Show Gist options
  • Save jboursiquot/7d36781085356481e36577b25a5d93c7 to your computer and use it in GitHub Desktop.
Save jboursiquot/7d36781085356481e36577b25a5d93c7 to your computer and use it in GitHub Desktop.
Jump into Go @ Baltimore Go User Group Meetup

Jump into Go @ Baltimore Go User Group

In this exercise your task is to work with the newline-delimited string below and count the occurrence of each character per line. You will need to make use of several features of of the Go programming language to accomplish this task, including variables, values, for and range, slices, maps, functions and reading from a file.

Success Criteria

  • Read the list of Go proverbs from proverbs.txt and handle potential errors.
  • Iterate through every proverb and count the number of times the character appears in that sentence.
  • Make use of the standard library packages only.
  • Your output should look like the sample below (though the sequence of characters will be different).

Bonus

  • Add tests that cover at least the character-counting behavior.
  • Make use of table-driven testing technique.
  • Create an executable that can accept a flag for the location of the file to read in.
  • Use goroutines to process each proverb for display.
  • Use goroutines and channels to communicate from a goroutine that does the character count to another goroutine that does the output.
Don't communicate by sharing memory, share memory by communicating.
Concurrency is not parallelism.
Channels orchestrate; mutexes serialize.
The bigger the interface, the weaker the abstraction.
Make the zero value useful.
interface{} says nothing.
Gofmt's style is no one's favorite, yet gofmt is everyone's favorite.
A little copying is better than a little dependency.
Syscall must always be guarded with build tags.
Cgo must always be guarded with build tags.
Cgo is not Go.
With the unsafe package there are no guarantees.
Clear is better than clever.
Reflection is never clear.
Errors are values.
Don't just check errors, handle them gracefully.
Design the architecture, name the components, document the details.
Documentation is for users.
Don't panic.

Sample Output

Don't communicate by sharing memory, share memory by communicating.
'y'=4, 'b'=2, 'n'=5, '''=1, ' '=8, 'i'=4, 's'=2, 'r'=4, ','=1, 'o'=5, 't'=3, 'c'=4, 'm'=8, 'u'=2, 'a'=4, 'e'=4, 'h'=2, 'D'=1, '.'=1, 'g'=2,

Concurrency is not parallelism.
'r'=3, 's'=2, 'y'=1, 'i'=2, 'm'=1, 'o'=2, 'u'=1, 'e'=2, 't'=1, 'p'=1, 'a'=2, '.'=1, 'C'=1, 'c'=2, ' '=3, 'n'=3, 'l'=3,

Channels orchestrate; mutexes serialize.
's'=4, 'r'=3, 'c'=1, 'x'=1, 'z'=1, '.'=1, 'i'=2, 'C'=1, 'a'=3, ' '=3, 'o'=1, 't'=3, ';'=1, 'm'=1, 'h'=2, 'n'=2, 'e'=7, 'l'=2, 'u'=1,

The bigger the interface, the weaker the abstraction.
't'=6, 's'=1, 'o'=1, '.'=1, 'h'=4, 'i'=3, 'g'=2, 'w'=1, 'e'=9, 'c'=2, ','=1, 'r'=4, 'n'=2, 'f'=1, 'a'=4, 'k'=1, 'T'=1, ' '=7, 'b'=2,

Make the zero value useful.
'M'=1, 'a'=2, 't'=1, 'v'=1, 'u'=3, 's'=1, 'h'=1, 'l'=2, 'f'=1, 'k'=1, 'o'=1, 'e'=5, ' '=4, 'z'=1, 'r'=1, '.'=1,

interface{} says nothing.
'i'=2, 'g'=1, ' '=2, 'y'=1, 'r'=1, 'c'=1, '{'=1, 's'=2, 'h'=1, 'n'=3, 'e'=2, 'a'=2, 'o'=1, '.'=1, 't'=2, 'f'=1, '}'=1,

Gofmt's style is no one's favorite, yet gofmt is everyone's favorite.
's'=6, 'e'=8, 'n'=3, ','=1, 'g'=1, 'f'=4, 'o'=7, ' '=10, 'i'=4, 'a'=2, 'G'=1, 'y'=3, 't'=6, '''=3, 'l'=1, 'v'=3, 'r'=3, '.'=1, 'm'=2,

A little copying is better than a little dependency.
'A'=1, 'o'=1, 'p'=2, 'a'=2, '.'=1, 'e'=7, 'c'=2, 'y'=2, 'n'=4, 'h'=1, 'd'=2, ' '=8, 't'=7, 'g'=1, 'b'=1, 'r'=1, 'l'=4, 'i'=4, 's'=1,

Syscall must always be guarded with build tags.
'S'=1, 'a'=5, 'u'=3, 'l'=4, 'e'=2, 'g'=2, 'd'=3, 'y'=2, 's'=4, ' '=7, 'w'=2, 'b'=2, 'i'=2, 'h'=1, '.'=1, 'c'=1, 'm'=1, 't'=3, 'r'=1,

Cgo must always be guarded with build tags.
'e'=2, 'd'=3, 'a'=4, 'w'=2, 'm'=1, 'u'=3, 'b'=2, 'i'=2, 'C'=1, ' '=7, 'r'=1, 'h'=1, '.'=1, 's'=3, 'l'=2, 't'=3, 'y'=1, 'g'=3, 'o'=1,

Cgo is not Go.
'g'=1, 'i'=1, 'n'=1, 'G'=1, '.'=1, 'C'=1, 'o'=3, ' '=3, 's'=1, 't'=1,

With the unsafe package there are no guarantees.
'o'=1, 'i'=1, 't'=4, 'e'=8, 'n'=3, 'g'=2, 'W'=1, 'h'=3, ' '=7, 's'=2, 'r'=3, 'u'=2, 'a'=6, 'p'=1, '.'=1, 'f'=1, 'c'=1, 'k'=1,

Clear is better than clever.
'i'=1, 'h'=1, 'n'=1, 'c'=1, 'e'=5, 'a'=2, 's'=1, 'b'=1, '.'=1, 'C'=1, 'l'=2, 't'=3, 'v'=1, 'r'=3, ' '=4,

Reflection is never clear.
'R'=1, 'l'=2, 'o'=1, ' '=3, 'r'=2, 'f'=1, 'c'=2, 't'=1, 'n'=2, 'i'=2, 's'=1, 'v'=1, 'a'=1, 'e'=5, '.'=1,

Errors are values.
'E'=1, 'r'=4, 'o'=1, 'e'=2, 'v'=1, 'u'=1, 's'=2, ' '=2, 'a'=2, 'l'=1, '.'=1,

Don't just check errors, handle them gracefully.
'y'=1, 'o'=2, 'e'=5, 'g'=1, 'm'=1, 'n'=2, 'j'=1, 'r'=4, 's'=2, 'c'=3, 'k'=1, 'a'=2, 'l'=3, 'D'=1, '''=1, 'u'=2, 'f'=1, '.'=1, ','=1, 'd'=1, 't'=3, ' '=6, 'h'=3,

Design the architecture, name the components, document the details.
'r'=2, 'm'=3, 'p'=1, '.'=1, 'g'=1, 'h'=4, 's'=3, 't'=8, 'u'=2, 'D'=1, 'e'=10, 'a'=3, 'd'=2, 'l'=1, 'i'=3, ' '=8, ','=2, 'o'=3, 'n'=5, 'c'=4,

Documentation is for users.
'f'=1, '.'=1, 'D'=1, 't'=2, ' '=3, 'c'=1, 's'=3, 'o'=3, 'n'=2, 'a'=1, 'i'=2, 'r'=2, 'u'=2, 'm'=1, 'e'=2,

Don't panic.
'o'=1, 't'=1, 'i'=1, '.'=1, 'D'=1, '''=1, ' '=1, 'p'=1, 'a'=1, 'c'=1, 'n'=2,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment