Go build-ldflags parameters and GDB debugging

Source: Internet
Author: User
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
    1. 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}
    2. You can search for identifiers by using Liteide to view binary files MyCat .

    3. 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
    1. Run print See file name, line number.

    2. 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.

    3. 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}  
    4. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.