Can someone help me understand how `bytealg.MakeNoZero()` in the Go standard 
library works, please?

https://github.com/golang/go/blob/master/src/internal/bytealg/bytealg.go#L118 
<https://github.com/golang/go/blob/master/src/internal/bytealg/bytealg.go#L118>

In the source it is a `func` without a body, and in my own code that won't 
compile in Go; I get "missing function body."  Where is its implementation and 
how does the Go compiler handle it?

I discovered `bytealg.MakeNoZero()` looking into the source for 
`strings.Builder`:

https://github.com/golang/go/blob/master/src/strings/builder.go#L61 
<https://github.com/golang/go/blob/master/src/strings/builder.go#L61>

I was considering following the advice in Go Proverbs that "A little copying is 
better than a little dependency" but I obviously found that I cannot copy it to 
my own package and compile it, at least not as-is.

I wanted to create my own version of `strings.Builder` that has a `Rewind()` 
method to allow slicing one or more characters from the internal `buf` buffer, 
e.g.:

https://github.com/golang/go/blob/master/src/strings/builder.go#L23 
<https://github.com/golang/go/blob/master/src/strings/builder.go#L23>

func (b *StringBuilder) Rewind(n int) {
   if n > len(b.buf) {
     n = len(b.buf)
   } 
   b.buf = b.buf[:len(b.buf)-n]
}

I could of course modify the code of `strings.Builder` to replace 
`bytealg.MakeNoZero()` but then I would have a less performant string builder, 
especially for larger strings:

https://github.com/golang/go/commit/132fae93b789ce512068ff4300c665b40635b74e 
<https://github.com/golang/go/commit/132fae93b789ce512068ff4300c665b40635b74e>

Any insight into `bytealg.MakeNoZero()` would be appreciated.

-Mike 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/FEC2DD8C-7664-425E-8905-A21D49257975%40newclarity.net.

Reply via email to