前情回顾:

Golang语言学习第1天_安装环境配置以及开发工具

一、本地调试

1、Go Demo

package main

import (
    "flag"
    "fmt"
)

// 声明输入参数hxpInputParam 默认值为0 用途描述
var hxpInputParam = flag.Int("hxpInputParam", 0, "Input parameter for 珩小派")

func main() {
    fmt.Println("珩小派-本地调试,配置演示开始")
    //解析命令行参数并设置相应的标志变量
    flag.Parse()
    var x = 5
    fmt.Println("The value of x is:", x)
    // 两数相加
    sum := *hxpInputParam + x
    fmt.Println("The sum of add x and hxpInputParam is:", sum)
    fmt.Println("珩小派-本地调试  End!")
}

2、GoLand Run/Debug Configurations

config-step1config-step2config-step3config-step4最终输出结果

珩小派-本地调试,配置演示开始
The value of x is: 5
The sum of add x and hxpInputParam is: 55
珩小派-本地调试  End!

二、附加到进程

1、Go Demo

package main

import (
    "flag"
    "fmt"
    "time"
)

var hxpInputParam1 = flag.Int("hxpInputParam1", 0, "Input parameter for 珩小派")

func main() {
    flag.Parse()
    var i = 0
    for {
        fmt.Println("print", i, *hxpInputParam1)
        i++
        time.Sleep(time.Second)
    }
}

2、安装gops 插件

执行以下命令

go install github.com/google/gops@latest

安装插件成功

gops-pluginGoLand-Run-Attach to Process

GoLand-Run-Attach to Process找到进程

Attach to Process附件到进程 ERROR

虽然已经运行Debug 但是无法获取Debug信息

could not attach to pid 2684: decoding dwarf section info at offset 0x0: too sho
rt - debuggee must not be built with 'go run' or -ldflags='-s -w', which strip d
ebug info

ERROR

3、编译并运行可执行文件

编译生成可执行文件

go build -gcflags="all=-N -l" -o StudyDebugProcess.exe编译

运行可执行文件

step1step2