On Mon, Jun 14, 2021 at 4:44 PM Axel Wagner <axel.wagner...@googlemail.com> wrote: > > Hi, > > On Mon, Jun 14, 2021 at 2:24 AM Amit Saha <amitsaha...@gmail.com> wrote: >> >> Hi - My main motivation to understand this is i always had to google >> this - how to convert a string to a byte slice. >> >> Is []byte a type that has been defined in the compiler? > > > No, but `byte` is. It is a predeclared identifier, referring to a builtin > integer type (it's an alias for `uint8`). > So the compiler knows what a `byte` is and what a slice is, so it knows what > a slice of byte is. > > The conversion between a slice of `byte`s and a `string` is then defined in > the spec: > >> A non-constant value x can be converted to type T in any of these cases: >> >> […] >> >> x is an integer or a slice of bytes or runes and T is a string type. >> >> x is a string and T is a slice of bytes or runes. > > This means the compiler, when seeing a conversion, explicitly tests for these > two cases. > > How the conversion then actually works, you can see in the compiler explorer. > The compiler emits a call into a function called `runtime.stringtoslicebyte`. > You can actually find that function in the source code of the runtime then > (the same function also contains other functions implementing similar > conversions). > Really, it just allocates a new `[]byte` of the appropriate size and then > copies over the bytes from the string. > >> Or, is that an internal level detail that an earlier stage (parsing) > > > You can actually mess with this a little bit to show that it's not done in > the parsing stage, but that the compiler actually knows if a type is a slice > of bytes or not. > Because `byte` is a predeclared identifier, it can be shadowed, like every > other identifier. That is, you can declare your *own* type called `byte`: > https://play.golang.org/p/6vDjw9eWX9s > You can also define your own alias for `uint8` and then convert that: > https://play.golang.org/p/nvseU7ofRru > Or you can do both - first shadown the builtin `byte` alias and *then* create > your own: > https://play.golang.org/p/1Y80stIxa5v > You can also define your own type called `uint8` and then define `byte` as an > alias to that and see that you can no longer convert: > https://play.golang.org/p/xUQrAmMm5Km > > All of this shows that the compiler really can't just rely on parsing and the > name. It really needs to have a notion of whether something is a slice of a > specific pre-declared type `uint8`, no matter what it is called in the source > code. > > It does that by creating a "virtual" package builtin inside the compiler and > then synthesizing type-definitions in that package. The code for that is here. > But it should be noted that this package doesn't really exist and behaves > significantly different from "normal" packages - not just because it is > implemented entirely in the compiler/runtime, but also because it's > identifier are defined "outside" any package, in the universe block. > > I assume this covers all questions :) Let us know if you have follow-ups :)
Thank you! That's enough for me to start digging in for a bit. > > Axel > >> takes care of when the compiler sees that statement? >> >> Thanks, >> Amit >> >> -- >> 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/CANODV3nBYmshDLFwUUdnnyVuvpjhWnwBJOb%3DwrZKEHXmtBgbSg%40mail.gmail.com. -- 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/CANODV3nw-RH1wdj4Zx2%2BY1UMojZSjFrXFBy_MA3rvmC-vW9bTQ%40mail.gmail.com.