跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Go 编程语言] goland 自动切换对应 dlv 版本调试程序,同时调试不同 go 版本的项目再也不用担心版本问题

发表于

公司项目用的可能不是最新 go 版本,而我自己折腾一般都是最新版本。但是 goland 没有找到根据不同项目自动切换 dlv 版本的方法。网上都是让 goland 编辑自定义属性 的 bin\idea.properties 文件添加 dlv.path=/xxx ,但这貌似是 goland 的全局配置,也就是所有项目在调试时都使用相同的 dlv 程序。

因此我编写如下代码,用来替换 goland 默认使用的 dlv 程序。该代码会自动识别调试的可执行程序编译的 go 版本,并执行对应 go 版本的 dlv 程序。

只需要在如下路径按照 go 大版本命名存放对应版本 dlv 程序即可。

$GOPATH/bin/dlv.go1.23.exe
$GOPATH/bin/dlv.go1.22.exe
$GOPATH/bin/dlv.go1.21.exe

goland 使用的 dlv 文件就用下面代码编译的可执行程序替换即可。

package main

import (
	"debug/buildinfo"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	var file string
	for i, v := range os.Args {
		if v == "exec" {
			file = os.Args[i+1]
			break
		}
	}
	info, err := buildinfo.ReadFile(file)
	if err != nil {
		panic(err)
	}
	vs := strings.Split(info.GoVersion, ".")

	cmd := exec.Command(
		// install dlv: $GOPATH/bin/dlv.go1.23.exe
		filepath.Join(os.Getenv("GOPATH"), "bin",
			fmt.Sprintf("dlv.%s.%s.exe", vs[0], vs[1])),
		os.Args[1:]...,
	)
	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr

	if err = cmd.Run(); err != nil {
		panic(err)
	}
}

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.