Course / The terminal and the language
Count the beads
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach 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 submitGoal
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. TheByteLengthfunction has a// TODOcomment and areturnline you will change. Wordis a variable that sits outside the function. The tests give it different values, so your function must readWordeach 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 stringor the linefunc 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
lencounts bytes. You already have a variable calledWord, so asklenabout it.len(Word)gives the number of bytes inWord. Replace thereturn 0line with areturnof that.- Write
return len(Word)insideByteLength. Leave thevar Word stringline alone.