How to find the source code for a method?

Some code I’m trying to grok has in it sbot.Network.Serve(ctx) which I assume means the “Serve” method receives whatever type the “Network” field is, in whatever struct type “sbot” is. My question is how would I reliably find out the source code where a method like “Serve” came from? I don’t know if I’m doing it right.

What I did was this: sbot is created with “mksbot.New()” and “mksbot” lets lets me know which module it’s from, thus I can find the source. As the module has about 20 files in it, I have to blindly rummage through them to tell exactly where the struct is defined, or New() for that matter, but it’s findable. Having found it, I can do the same for sbot.Network, until I find the struct definition, and in the same file is a “Serve()” function receiving that type.

If I see something like obj.Field.Method(...) is it guaranteed that Method() is defined in the same file as the type of obj.Field? Or is it at least guaranteed that Method() is defined in the same directory as it? Is there some standard to the filename in that directory where things are located? In the “ssb” example, they’re located in a file named “new.go” but is that just arbitrary convention?

I’m worried that someone could define a method that receives a given type, in a totally independent third party module from that type. Since methods are not qualified with module namespace identifiers, there would be no way to find out which module that method came from, and thus no way to find the method’s source, outside of grep -drecurse ~/go/pkg/mod --include '*.go' -e 'func.*Method( That’s not true, is it?

Methods cannot be defined on types in other packages, so the method will definitely be in the same folder as its type, but not necessarily in the same file. I think it makes sense sometimes: I have done this before, and my own use case was that I wrote a wrapper around the database/sql package and its *sql.DB.QueryContext method that builds queries from expression trees and that logic by itself was quite long, so I put it in its own query.go file instead of leaving it all in the db.go file.

Anyway, you can install a tool called godoc with this command:

go install


  1. Change directory to your project
  2. Run godoc -http :8080. Leave it running.
  3. Open a browser and go to http://localhost:8080

From there, you can navigate the packages on your computer similarly to More importantly, when you click on a function, method, or type name, It will take you to the line where that function, method, variable, type, whatever, is defined.

Thank you! That’s a relief. It’s a bit tricky to search through all the files in a single folder, but certainly less tricky than recursive multi-package file tree searches. I’ll try godoc too that sounds fantastic! Maybe I won’t need to be clueless around here as much.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.