This is a creation in
Article, where the information may have evolved or changed.
Go build-ldflags parameters and GDB debugging
Sometimes when publishing we want to hide all the code implementation related information, using the go build-ldflags parameter can achieve the relevant requirements.
Sample code
This article uses the version of Go 1.6.
package mainimport"log"typestruct { string age int}func main() { cat := MyCat{ "tom", age: 10, } log.SetFlags(log.Llongfile) log.Println(cat)}
Using-ldflags
Set compilation parameters -ldflags "-w -s" .
This is to remove the -w debug information (Cannot use GDB debugging), in -s order to remove the symbol table (not clearly specific effect).
go build -ldflags "-w -s" ./hello.go
-
Run Watch print, see the file name, line number and other information is still retained.
./hello
The print output is as follows. And the executable is placed in any directory, and the same output is displayed.
Visible, the file name and line number compile with the current file name and line number compiled into the binary.
/home/gv/hello.go:17: {tom 10}
-
You can search for identifiers by using Liteide to view binary files MyCat .
-
Unable to debug with GDB, visible debug information cleared
gdb ./hello(gdb) l main.mainNo symbol table is loaded. Use the "file" command.
Do not use-ldflags
go build ./hello.go
-
Run print See file name, line number.
-
Use liteide to view a binary file to search for the mycat identifier.
These are the same results as those obtained using the-ldflags parameter.
-
uses GDB debugging to list the source code, or to break point stepping debugging.
(gdb) L MAIN.MAIN5 type mycat struct {6 name String7 age int8}910 func main () {one C At: = mycat{12 Name: "Tom", age:10,14}
-
Delete the source file and then use GDB debugging, you can break point stepping debugging, but cannot column Source code, only the travel number and file name can be listed.
gdb./hello (GDB) l main.main5/home/gv/hello.go:no such file or directory. In/home/gv/hello.go
Visible, debug if you want to list source code, you also need to find the file name and line number that you want to write at compile time.