godebug

package
v0.4.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package godebug is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package godebug holds the pieces of running a game under the Go debugger: parsing Delve's listen announcement from nsgo dev -debug output and building the IDE attach configuration for it. Both game dev and the editor use it, so the configuration a CLI session prints and the one the editor copies or writes to .vscode/launch.json are the same.

Index

Examples

Constants

View Source
const AttachConfigName = "Attach to game"

AttachConfigName is the name of the VS Code launch configuration, and the suggested GoLand Go Remote configuration, that attaches Delve to a running game.

Variables

This section is empty.

Functions

func AttachConfig

func AttachConfig(host string, port int) attachConfig

AttachConfig returns the attach configuration for the Delve server at host:port, named AttachConfigName. GoLand's equivalent is a Go Remote configuration with the same host and port.

func AttachConfigJSON

func AttachConfigJSON(host string, port int) string

AttachConfigJSON returns AttachConfig as an indented JSON object, ready to paste into a .vscode/launch.json configurations array.

func ParseListenAddress

func ParseListenAddress(line string) (host string, port int, ok bool)

ParseListenAddress extracts the host and port from Delve's listen announcement line. ok is false for any other line.

Example

ExampleParseListenAddress turns Delve's announcement into an IDE attach configuration.

package main

import (
	"fmt"

	"github.com/nsgo-dev/game/pkg/tooling/godebug"
)

func main() {
	host, port, ok := godebug.ParseListenAddress("API server listening at: 127.0.0.1:40000")
	fmt.Println(host, port, ok)
	_, _, ok = godebug.ParseListenAddress("building game...")
	fmt.Println(ok)
	fmt.Println(godebug.AttachConfigJSON(host, port))
}
Output:
127.0.0.1 40000 true
false
{
  "name": "Attach to game",
  "type": "go",
  "request": "attach",
  "mode": "remote",
  "debugAdapter": "dlv-dap",
  "host": "127.0.0.1",
  "port": 40000
}

func UpsertVSCodeLaunchConfig

func UpsertVSCodeLaunchConfig(projectDir, host string, port int) error

UpsertVSCodeLaunchConfig writes the AttachConfigName configuration for the Delve server at host:port into projectDir/.vscode/launch.json, creating .vscode and the file when they are missing and replacing the entry of the same name when one is there. Other configurations and top-level members are preserved. A launch.json with comments or trailing commas (VS Code allows them) cannot be parsed and is reported as an error rather than rewritten.

Example

ExampleUpsertVSCodeLaunchConfig adds the attach configuration to an existing launch.json and later updates its port in place.

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"

	"github.com/nsgo-dev/game/pkg/tooling/godebug"
)

func main() {
	dir, err := os.MkdirTemp("", "godebug-example")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer os.RemoveAll(dir)
	_ = os.MkdirAll(filepath.Join(dir, ".vscode"), 0o755)
	existing := `{"version": "0.2.0", "configurations": [{"name": "Run tests", "type": "go"}]}`
	_ = os.WriteFile(filepath.Join(dir, ".vscode", "launch.json"), []byte(existing), 0o644)

	fmt.Println(godebug.UpsertVSCodeLaunchConfig(dir, "127.0.0.1", 40000))
	fmt.Println(godebug.UpsertVSCodeLaunchConfig(dir, "127.0.0.1", 40001))

	data, _ := os.ReadFile(filepath.Join(dir, ".vscode", "launch.json"))
	var file struct {
		Configurations []struct {
			Name string `json:"name"`
			Port int    `json:"port"`
		} `json:"configurations"`
	}
	_ = json.Unmarshal(data, &file)
	for _, c := range file.Configurations {
		fmt.Printf("%s port=%d\n", c.Name, c.Port)
	}
}
Output:
<nil>
<nil>
Run tests port=0
Attach to game port=40001

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL