Course outline
Course / The terminal and the language

Count the beads

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each lab: download it, edit the file named in the instructions, then submit.

gonomicon fetch week-1/09-01-bytes-not-letters
cd 09-01-bytes-not-letters
gonomicon submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal

Fill in ByteLength so it returns the number of bytes in the string stored in Word.

Example

If Word held "cat", ByteLength() would return 3, one byte per letter. If Word held "né", it would return 3 too: n is one byte, and é is two.

Word = "né"
fmt.Println(ByteLength()) // 3

Rules

  • Edit bytes.go. The ByteLength function has a // TODO comment and a return line you will change.
  • Word is a variable that sits outside the function. The tests give it different values, so your function must read Word each time it runs. Do not copy a value into a new variable at the top of the file.
  • Do not change the line var Word string or the line func ByteLength() int {.
  • Do not add imports. You will not need any.
  • Count bytes, not symbols. The tests include accents and emoji on purpose.

What you'll practice

Remembering that a Go string is a row of bytes, and that len counts those bytes.

Run it

gonomicon fetch week-1/09-01-bytes-not-letters
cd 09-01-bytes-not-letters
gonomicon test
gonomicon submit

Hints

  1. len counts bytes. You already have a variable called Word, so ask len about it.
  2. len(Word) gives the number of bytes in Word. Replace the return 0 line with a return of that.
  3. Write return len(Word) inside ByteLength. Leave the var Word string line alone.

Something wrong with this lesson? Report a problem