A collection of various examples of the Go Programming Language.
Most of the examples (with the exception of the unit tests) can be run via go run
, eg. go run slices/leftrotation.go
.
- leftrotation.go demostrates two algorithms for rotating an array (or slice, in this case) to the left by an abitrary number of positions. See the /slices directory for more examples with slices.
- parity.go determines the parity of the given integer, returning 1 if the number of ones in the binary string is odd, other wise 0 if even. See the /bits directory for more bitwise stuff.
- twosum.go uses a map to solve the two-sum problem, which is to find which two (if any) numbers in an array sum up to the given total. See the /maps directory for more map shenanigans.
- compress.go takes a string and compresses it using run-length encoding. See the /strings directory for more string-based functions.
- operate.go demostrates polymorphism through an abstract
Vehicle
class vcl.go. Try out eat.go and measure.go to see other examples of the structs in /structs in action.
- race.go will spawn a handful of goroutines to show how concurrency is handled in go. See the /goroutines directory for more.
- sync.go uses goroutines and channels to wait for responses from two (simulated) external dependencies. See the /channels directory for more on how to use channels in Go.
- sum_test.go demostrates a simple unit test suite. First
cd tests
, then rungo test
to run the test file in that directory. Optionally, add the-v
flag at the end to get a verbose pass/fail message for each test.