Skip to content

cmd/go2go: local type as type parameter fails #39738

@DeedleFake

Description

@DeedleFake

This makes sense given the way that go2go works, but it should probably at least give a clearer error message that explains that the tool doesn't support that.

Playground

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"strings"
)

func Decode(type T)(r io.Reader) (T, error) {
	var t T
	err := json.NewDecoder(r).Decode(&t)
	return t, err
}

func main() {
	type s struct {
		This string `json:"this"`
		A    string `json:"a"`
	}

	t, err := Decode(s)(strings.NewReader(`{"this": "is", "a": "test"}`))
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v", t)
}

Output:

# play
./prog.go2:22: undefined: s
./prog.go2:11: undefined: s

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions