Golang type inderection

I was curious how Prometheus scrapes targets at regular intervals and started with config.go. There I found the following strange code:

unmarshal extract from config.go

It turns out that’s used to purify structure i.e. get rid of its methods. To illustrate how it works look at the following code.

package main

import "fmt"

type Data struct {
	X int
}

func (d *Data) String() string {
	return fmt.Sprintf("data value is: %d", d.X)
}

func main() {
	d := &Data{}
	fmt.Printf("1. %s (%p)\n", d, d)
	// 1. Declare type alias without methods.
	type plain Data	
	// 2. Convert d pointer to a pointer  to plain 
	dp := (*plain)(d)
	fmt.Printf("2. %s (%p)\n", dp, dp)
}

gives output like:

1. data value is: 0 (0xc0000200d8)
2. &{\%!s(int=0)} (0xc0000200d8)

Both pointers have the same address but different types.