count-strings

command
v0.0.0-...-52caca7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2023 License: GPL-3.0-or-later Imports: 0 Imported by: 0

Documentation

Overview

Write a program that takes a list of strings on the command line and counts the occurrence of each string. Then output the strings and their counts like this:

bar - 1 foo - 2 quux - 1

You can use fmt.Printf() to print these out with a format of "%s - %d\n"

The output should be sorted by the strings (not their counts). You'll need to use the core sort package for sorting.

If no strings are given at all, log a fatal error with this message:

This program expects at least 1 argument

Note that Go does not let you declare a map variable and then start assigning to it:

var foo map[int]float
foo[42] = 42.0

This will blow up with an error like "assignment to entry in nil map"

You can either use make() or create an empty map:

foo := make(map[int]float)
// The {} creates an empty (but initialized) map
foo := map[int]float{}

The use of make() is more idiomatic and what you'll see in most Go code.

To run the tests, run "go build", then "go test -v".

> go build && go test -v

You're done when all the tests pass.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL