It is defined in the runtime and go:linkname'd into the bytealg package: https://github.com/golang/go/blob/90bcc552c0347948166817a602f612f219bc980c/src/runtime/slice.go#L394
>From the spec <https://go.dev/ref/spec#Function_declarations>: > A function declaration without type parameters may omit the body. Such a > declaration provides the signature for a function implemented outside Go, > such as an assembly routine How that works is, of course, implementation defined. It may be using assembly or, as in this case, with the body being provided by the linker later. On Wed, 26 Jun 2024 at 08:38, Mike Schinkel <m...@newclarity.net> wrote: > 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 > > 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 > > 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 > > 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 > > 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 > <https://groups.google.com/d/msgid/golang-nuts/FEC2DD8C-7664-425E-8905-A21D49257975%40newclarity.net?utm_medium=email&utm_source=footer> > . > -- 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/CAEkBMfGb_14u9fo-dki-Cv%2BTZG5pBGZX%3DT6VN8KNQPJdwmv8cA%40mail.gmail.com.