builtin function definitions

171 views
Skip to first unread message

Shane H

unread,
Jul 28, 2020, 5:39:53 PM7/28/20
to golang-nuts
Hi all, I'm trying to understand what *exactly* the .(type) is doing in the following statement

switch foo := bar.(type)

I mean, I get that foo is being assigned a type converted version of the bar interface, but, I want to see what exactly they .(type) call does.

I have found https://github.com/golang/go/blob/master/src/go/types/selection.go#L60 which I *think* is the method being called, but I am not sure.

So I have two questions.
1) Am I looking at the correct function
2) (and far more importantly) How do I find which method such code is calling (it's problematic for me at this point towork out what, for example, something defined in builtin is really calling.

Can someone point me at a resource that I have obviously overlooked?


Note: I've seen this https://stackoverflow.com/questions/18512781/built-in-source-code-location and, rereading it this morning it looks like "If it's not in the runtime package, start grepping the compiler packages" - is that what I should be doing?

tokers

unread,
Jul 28, 2020, 10:20:17 PM7/28/20
to golang-nuts
You may try to use `go tool compile -S <filename>` and read the assemble codes to find the truth.

Shane H

unread,
Jul 28, 2020, 10:40:18 PM7/28/20
to golang-nuts
Thanks, I have a bash script that I use to examine the assembly code that is generated by a given go file 
https://bpa.st/MRJ4IWJFJ5YNHXFLCMMYGGSI3A

But that takes me past the code that was used to generate that assembly

I'm wanting the midpoint here, the code that the compiler sees, in order to generate the assembly.

Shane H

unread,
Jul 28, 2020, 10:42:10 PM7/28/20
to golang-nuts
Hrm, that bpaste site will only last a week, so, for posterity, I'll paste the script here

#!/bin/bash if [[ "$#" -ne 1 ]]; then echo "No filename supplied, nothing to do" exit 0 fi INFILE="$1" echo "======================= In file =======================" cat $INFILE echo "========================================================" # See assembly code generated by compiler echo "============= ASM generated in compilation =============" GOOS=linux GOARCH=amd64 go tool compile -S $INFILE echo "========================================================" # What's in the binary after linking echo "==================== After Linker ======================" go build -o x.exe $INFILE go tool objdump -s main.main x.exe echo "========================================================" echo "Cleaning up" rm -i x.exe rm -i $(echo "$INFILE"| sed "s/go$/o/")

Martin Schnabel

unread,
Jul 29, 2020, 5:23:22 AM7/29/20
to Shane H, golang-nuts
This is a special kind of switch called a type switch. You can read more
about it in the language specification where its part of the intrinsic
go syntax. https://golang.org/ref/spec#Switch_statements

Because it is a special language construct you need to look at the
compiler. You probably want to check out the default gc compiler and may
start your journey here:
https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/swt.go#L617

you can use your editor with go guru or gopls to navigate references.

Have fun!
> --
> 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...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/e52c6dbc-1cb8-44c1-a45b-be93c89bb1b7o%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/e52c6dbc-1cb8-44c1-a45b-be93c89bb1b7o%40googlegroups.com?utm_medium=email&utm_source=footer>.

Shane H

unread,
Jul 29, 2020, 5:21:23 PM7/29/20
to golang-nuts


On Wednesday, July 29, 2020 at 7:23:22 PM UTC+10, mb0 wrote:
This is a special kind of switch called a type switch. You can read more
about it in the language specification where its part of the intrinsic
go syntax. https://golang.org/ref/spec#Switch_statements

Because it is a special language construct you need to look at the
compiler. You probably want to check out the default gc compiler and may
start your journey here:
https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/swt.go#L617

This is really helpful, thanks.

The two questions I am left with are:
How do I recognise that something is a "special language construct"; is it simply a matter of, I cannot ctrl-] to it directly, so I need to grep?


And, just as importantly, how do I find out what I need to grep /for/

Regards
 

Martin Schnabel

unread,
Jul 29, 2020, 7:20:54 PM7/29/20
to Shane H, golang-nuts
The linked spec is the the definitive language specification. I read it
carefully a couple of time (cannot recommend it enough) and knew about
type switches.

The most common language implementation is go's default compiler gc.
Which is the place to implement the language spec and type switches.

I knew that the go command is used to compile with the gc compiler. I
opened the go src and looked at the cmd package, finding the compile and
gc folder and finally the swt.go file. So I didn't even need grep.

Hope that helps!

Again, my recommendation: the go language spec is really very well
written and very readable for a language spec.
> --
> 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...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/5d2cfcd6-579a-4846-8c33-0aa763a9efd9o%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/5d2cfcd6-579a-4846-8c33-0aa763a9efd9o%40googlegroups.com?utm_medium=email&utm_source=footer>.

jake...@gmail.com

unread,
Jul 30, 2020, 8:20:06 AM7/30/20
to golang-nuts
The good news is that Go is simpler than many other languages, with fewer constructs, concepts and corner cases. So after using it for a while, you will rarely bump into anything "new".
Reading the language spec is great, and I have done it myself a couple of times. But, depending on your level of programming experience, it may be a bit difficult to pull useful information from the spec, and can be a bit tedious to read. For starters, I strongly suggest working through A Tour of Go, maybe a few times. It covers almost all the language concepts. Type switches are covered in in the tour at: https://tour.golang.org/methods/16.
Reply all
Reply to author
Forward
0 new messages