(I'm learning Go using Donovan and Kernighan's The Go Programming Language. The answer to this will probably be painfully obvious to others, but I'm stumped and not sure where to begin.)
As an exercise in GOPL, the authors ask the reader to modify their reverse program (which reverses a slice of ints in place) "to reverse the characters of a []byte slice that represents a UTF-8 encoded string, in place" (93). They add: "Can you do it without allocating new memory?"
In a nutshell, I want to ask whether the following allocates new memory. I think that it doesn't, based on the outcome of the print statements, but I'm not sure. Another way to put my confusion is this: if the reverse method reverses in place, I would expect it not to allocate new memory. So, I'm assuming that I must be missing something because they ask for the method to work in place and then add the challenge of not allocating new memory. Are they nudging me to avoid something I've done? Is there some extra trick here worth knowing about memory allocation (in Go or in general)?
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
phrase := "Hello, 世界!"
fmt.Printf("Before reverse:\tmemory address %p => phrase: %s\n", &phrase, phrase)
phrase = string(reverseByRune([]byte(phrase)))
fmt.Printf("After reverse:\tmemory address %p => phrase: %s\n", &phrase, phrase)
}
func reverseByRune(b []byte) []byte {
for i := 0; i < len(b); {
_, size := utf8.DecodeRune(b[i:])
reverse(b[i : i+size])
i += size
}
reverse(b)
return b
}
func reverse(b []byte) []byte {
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
return b
}
Here it is on the Go Playground: https://play.golang.org/p/Qn7nYXLGoQn.
PS I can only upvote and accept once, but if anyone wants extra thanks, I'd love any links about memory allocation (especially in Go).