Skip to content

Instantly share code, notes, and snippets.

@jboursiquot
Last active December 4, 2018 17:14
Show Gist options
  • Save jboursiquot/56b481f8bbe648bde603349bf158340a to your computer and use it in GitHub Desktop.
Save jboursiquot/56b481f8bbe648bde603349bf158340a to your computer and use it in GitHub Desktop.
GoBridge Baltimore Workshop Challenge Solutions
// Challenge 1
package main
import (
"fmt"
"strings"
)
const proverbs = `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.`
func main() {
lines := strings.Split(proverbs, "\n")
for _, l := range lines {
fmt.Printf("%s\n", l)
for k, v := range charCount(l) {
fmt.Printf("'%c'=%d, ", k, v)
}
fmt.Print("\n\n")
}
}
func charCount(line string) map[rune]int {
m := make(map[rune]int, 0)
for _, c := range line {
m[c] = m[c] + 1
}
return m
}
// Challenge 2
package main
import (
"fmt"
"io/ioutil"
"strings"
)
func main() {
bs, err := ioutil.ReadFile("proverbs.txt")
if err != nil {
panic(fmt.Errorf("failed to read file: %s", err))
}
proverbs := string(bs)
lines := strings.Split(proverbs, "\n")
for _, l := range lines {
fmt.Printf("%s\n", l)
for k, v := range charCount(l) {
fmt.Printf("'%c'=%d, ", k, v)
}
fmt.Print("\n\n")
}
}
func charCount(line string) map[rune]int {
m := make(map[rune]int, 0)
for _, c := range line {
m[c] = m[c] + 1
}
return m
}
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.
// Challenge 3
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)
type proverb struct {
line string
chars map[rune]int
}
func (p *proverb) charCount() map[rune]int {
if p.chars != nil {
return p.chars
}
m := make(map[rune]int, 0)
for _, c := range p.line {
m[c] = m[c] + 1
}
p.chars = m
return p.chars
}
func main() {
path := pathFromFlag()
if path == "" {
path = pathFromEnv()
}
if path == "" {
fmt.Println("You must specify one the file path with -f or as FILE environment variable.")
os.Exit(1)
}
proverbs, err := loadProverbs(path)
if err != nil {
fmt.Printf("Failed to load proverbs: %s", err)
os.Exit(1)
}
for _, p := range proverbs {
fmt.Printf("%s\n", p.line)
for k, v := range p.charCount() {
fmt.Printf("'%c'=%d, ", k, v)
}
fmt.Print("\n\n")
}
}
func pathFromFlag() string {
path := flag.String("f", "", "file flag")
flag.Parse()
return *path
}
func pathFromEnv() string {
return os.Getenv("FILE")
}
func loadProverbs(path string) ([]*proverb, error) {
var proverbs []*proverb
bs, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
lines := strings.Split(string(bs), "\n")
for _, line := range lines {
p := &proverb{line: line}
proverbs = append(proverbs, p)
}
return proverbs, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment