Sha256: d4a91934ae868402ca05406367dbf0ba54b453a27a1a7cdd3ee717823797a0e9

Contents?: true

Size: 1.45 KB

Versions: 152

Compression:

Stored size: 1.45 KB

Contents

## Implementation

Define two functions: (Two? Yes, sometimes we ask more out of Go.)

All returns a list of all substrings of s with length n.

`All(n int, s string) []string`

UnsafeFirst returns the first substring of s with length n.

`UnsafeFirst(n int, s string) string`

At this point you could consider this exercise complete and move on.

But wait, maybe you ask a reasonable question: Why is the function
called **Unsafe** First?  If you are interested, read on for a bonus
exercise.

### Bonus exercise:

Once you get `go test` passing, try `go test -tags asktoomuch`.  This
uses a *build tag* to enable a test that wasn't enabled before. Build
tags allow for the selection of files to be used by the package. In
our case it will control which test files are used. You can read more
about those at
[the Go documentation](https://golang.org/pkg/go/build/#hdr-Build_Constraints).

You may notice that you can't make this *asktoomuch* test happy. We
need a way to signal that in some cases you can't take the first N
characters of the string. UnsafeFirst can't do that since it only
returns a string.

To fix that, let's add another return value to the function.  Define

`First(int, string) (first string, ok bool)`

and test with `go test -tags first`.

The `ok bool` second return argument is a common and idiomatic pattern
in Go. For example you see it in
[Map lookups](https://blog.golang.org/go-maps-in-action) and
[type assertions](https://tour.golang.org/methods/15).

Version data entries

152 entries across 152 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.179 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.178 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.177 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.176 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.175 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.174 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.173 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.172 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.171 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.170 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.169 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.167 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.166 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.165 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.164 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.163 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.162 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.161 tracks/go/exercises/series/.meta/hints.md
trackler-2.2.1.160 tracks/go/exercises/series/.meta/hints.md