Hi, I’m Mingo.

In their learning Golang this time, I wrote a detailed study notes on my personal number WeChat public Go programming time, for the language, I was a beginner, so writing should fit in with the new to classmates, if you are just learning the language, don’t focus on prevention, study together, Grow together.

My Github:github.com/iswbm/GolangCodingTime my online blog: golang.iswbm.com


As a novice, master a good debugging tool, for our language learning or troubleshooting, very helpful.

If you use VS Code or Goland, you can get started, so I’m not going to write about it anymore.

In fact, I prefer simple and direct command line debugging to IDE debugging tools with user interfaces for three reasons:

  1. Fast speed, personally feel under Windows speed is very slow
  2. Less dependencies and easy debugging on Linux servers
  3. The instructions are simple, and I’m used to just using the shortcut keys

If you have the same feelings and habits as I do, check out today’s article about GDB debugging tools.

1. Download and install Go

To debug on Linux, we need to install Go first, because the first section only describes the download and installation of Windows, not how to install Linux. So I’m going to tell you about it, because I’ve already installed it and I can skip it.

First go to the Go download page (golang.org/dl/), view and copy the source…

Log on to your Linux machine and download using Wget

$wget HTTP: / / https://dl.google.com/go/go1.14.2.linux-amd64.tar.gzCopy the code

Decompress the downloaded source package to the /usr/local directory and set environment variables

[root@localhost ~]# tar -c /usr/local-xzf go1.14.2.linux-amd64.tar.gz [root@localhost ~]# [root@localhost ~]# export PATH=$PATH:/usr/local/go/bin [root@localhost ~]# which go /usr/local/go/bin/go [root@localhost ~]# [root@localhost ~]# Go version go Version go1.14.2 Linux/AMD64 [root@localhost ~]#Copy the code

2. Start debugging

GDB is used for debugging (version 7.1 + is required), please make sure GDB is installed on your machine before using it

[root@localhost code]# which gdb
/usr/bin/gdbCopy the code

When you’re ready, write a test file in your directory

package main

import "fmt"

func main(){
  msg := "hello, world"
  fmt.Println(msg)
}Copy the code

Then execute the following command to compile, there are a lot of parameters, there are questions can search engine

$go build-gcflags "-n -l" demo. GoCopy the code

Finally, run the GDB command to enter the debugging interface

$GDB -tui demo if you like this interface, use this command $GDB demoCopy the code

The complete operation is as follows:

When you enter the GDB debug screen, it is not immediately available. You need to enter first, then you type in a few lines of command, and the debug window will display the code.

Breakpoint 1 at 0x4915c0: The file/home/wangbm/code/demo. Go to line 5. (GDB) run # execution process Starting the program: /home/wangbm/code/demo Breakpoint 1, main.main () at /home/wangbm/code/demo.go:5 (gdb)Copy the code

3. Explain debugging instructions in detail

To be proficient with GDB, you need to be familiar with its commands, which are listed here

  • R: run, execute the program

  • N: Next, the next step, do not enter the function

  • S: step, the next step, will go to the function

  • B: Breakponit, set a breakpoint

  • L: list, check source code

  • C: continue to the next breakpoint

  • Bt: backtrace to view the current call stack

  • P: print to view variables

  • Q: Quit: exits GDB

  • Whatis: View the object type

  • Info breakPoints: View all breakpoints

  • Info locals: Views local variables

  • Info args: View the parameter values of the function and the variable values to return

  • Info frame: stack frame information

  • Info Goroutines: Displays information about goroutines. Before use, first need to pay attention to perform the source/usr/local/go/SRC/runtime/runtime – GDB. Py

  • Goroutine 1 bt: View the Goroutine call stack with the specified serial number

  • Enter: Repeat the previous operation

There are several instructions that are flexible to use

Like l-list, look at the code

8 # Check the number of lines in the specified file (GDB) l 5. 8 # Check the number of lines in the specified file (GDB) l 5. 8 # Check the number of lines in the specified file (GDB) l 5Copy the code

Replace the top L with the b, and most of them will work as well

Go :8 # In the specified function break point, remember to add the package name b main.mainCopy the code

And p-print, print variables

P $cap(var) p $dType (var) iface var # For example: (GDB) p I $4 = {STR = "CBB "} (GDB) whatis I type = regexp.input (GDB) p $dtype(I) $26 = (struct regexp.inputBytes *) 0xf8400b4930 (gdb) iface i regexp.input: struct regexp.inputBytes *Copy the code

The above is about the use of GDB, very simple, you can manually click the experience.

Refer to the article

  • Go official instructions for GDB usage
  • Mac debugs golang