Get the App
SLTechnology News&Howtos  ›  Development  › 

How to dump GOSSAFUNC diagrams in Go

Shulou Source: shulou.com Published: 2022-06-02 19:53:32 09月30日 Update

This article mainly introduces "how to dump GOSSAFUNC diagrams in Go". In daily operation, I believe many people have doubts about how to dump GOSSAFUNC diagrams in Go. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to dump GOSSAFUNC diagrams in Go". Next, please follow the editor to study!

The SSA back end of the Go compiler includes a tool that generates HTML debug output at compile time. This article shows how to print SSA output for functions and methods.

Let's start with a sample program that contains functions, value methods, and pointer methods:

Package main import ("fmt") type Numbers struct {vals [] int} func (n * Numbers) Add (v int) {n.vals = append (n.vals, v)} func (n Numbers) Average () float64 {sum: = 0.0 for _ Num: = range n.vals {sum + = float64 (num)} return sum / float64 (len (n.vals))} func main () {var numbers Numbers numbers.Add (200) numbers.Add (43) numbers.Add (- 6) fmt.Println (numbers.Average ())}

SSA debug output is controlled by the GOSSAFUNC environment variable. This variable contains the name of the function to be dumped. This is not the fully qualified name of the function. For the above func main, the function name is main instead of main.main.

% env GOSSAFUNC=main go buildruntimedumped SSA to. /.. / go/src/runtime/ssa.htmltdumped SSA to. / ssa.html

In this example, GOSSAFUNC=main matches both main.main and a function named runtime.main. [1] this is a bit unlucky, but in fact it may not be a big deal, because if you want to make performance adjustments to the code, it won't appear in the huge spaghetti chunks in func main.

Your code is more likely to be in the method, and you may have read this article and looked for SSA output that can dump the method.

To print SSA debugging for the pointer method func (n * Numbers) Add, the equivalent function is called (* Numbers) .Add: [2]

% env "GOSSAFUNC= (* Numbers) .add" go buildtdumped SSA to. / ssa.html

To print SSA debugging for the value method func (n Numbers) Average, the equivalent function is named (* Numbers) .average, even if it is a value method:

% env "GOSSAFUNC= (* Numbers) .average" go buildtdumped SSA to. / ssa.html

If you do not build Go from source code, the path to the runtime package may be read-only and you may receive an error message. Please do not use sudo to resolve this problem.

Note the shell reference.

At this point, the study of "how to dump GOSSAFUNC diagrams in Go" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Methods functions learning output articles code variables names pointers more problems help compilation practical huge big deal and then nothing this is not an example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information OPPO Reno NVidia Apple