type path []byte
func (p path) ToUpper() {
for i, b := range p {
if 'a' <= b && b <= 'z' {
p[i] = b + 'A' - 'a'
}
}
}
func main() {
pathName := path("/usr/bin/tso")
pathName.ToUpper()
fmt.Printf("%s
", pathName)
}
[Exercise: Convert the ToUpper method to use a pointer receiver and see if its behavior changes.]
how to use a pointer method ? i have tried to dereference *p and tried to delete i from range but it keeps saying mismatched types.
Since path
is a type defined on a []byte
which happens to be a slice, there is no need to use a pointer receiver since slice types are already reference types.
However, if a pointer receiver is required, you'll need to dereference the pointer value everywhere in your method to get the underlying slice value:
func (p *path) ToUpper() {
for i, b := range *p { // dereference p with a * to get the
// underlying []byte slice
if 'a' <= b && b <= 'z' {
(*p)[i] = b + 'A' - 'a' // derefernce p here as well
}
}
}
Working code: https://play.golang.org/p/feqeAlb80z