5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

go.modの参照先をローカルのパスに向けてデバッグする

Posted at

Goは依存ライブラリを go.mod で管理しますが、依存ライブラリの中にバグがあった場合に、一時的にライブラリコード内にデバッグ用のコードを差し込みたいことがあります。

例えば、 github.com/hashicorp/hcl/v2 => /path/to/hcl に参照先を変えるには、こんなかんじで go.modreplace を書きます。手元の環境はGo 1.14です。

go.mod
module tmp

go 1.14

require (
	github.com/davecgh/go-spew v1.1.1
	github.com/hashicorp/hcl/v2 v2.6.1-0.20200915195656-bf0a7fe4fe09
	github.com/zclconf/go-cty v1.2.0
)

replace github.com/hashicorp/hcl/v2 => /path/to/hcl
main.go
package main

import (
	"fmt"

	"github.com/davecgh/go-spew/spew"
	"github.com/hashicorp/hcl/v2"
	"github.com/hashicorp/hcl/v2/hclwrite"
)

func main() {
	src := `
resource "foo_bar" "baz" {
  disabled = (true)
}
`
	f, _ := hclwrite.ParseConfig([]byte(src), "", hcl.Pos{Line: 1, Column: 1})
	got := f.BuildTokens(nil)
	fmt.Printf("got:\n%s\n", string(got.Bytes()))
	fmt.Printf("dump:\n%s\n", spew.Sdump(got))
}

あとは /path/to/hcl のコードを適当にいじればよいだけ。

参考: https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?