Skip to content

encoding/xml: Unmarshal panic when use generic #48318

@nzlov

Description

@nzlov

What version of Go are you using (go version)?

$ go version
go version devel go1.18-c69f5c0d76 Fri Sep 10 10:45:59 2021 +0000 darwin/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/nzlov/Library/Caches/go-build"
GOENV="/Users/nzlov/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/nzlov/workspace/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/nzlov/workspace/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/Users/nzlov/program/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/nzlov/program/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="devel go1.18-c69f5c0d76 Fri Sep 10 10:45:59 2021 +0000"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/nzlov/workspace/vgo/generices/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/71/4t6lk06x5y77f2wyln9vqnlw0000gn/T/go-build3474694710=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
)

type A[T any] struct {
	Name          string  `json:"name" xml:"name"`
	Data          T       `json:"data" xml:"data"`
	ErrorResponse ErrResp `json:"errorResponse" xml:"errorResponse"`
}

type ErrResp struct {
	Age int `xml:"age"`
}

func main() {
	fmt.Printf("%+v\n", *b[int]())
}

func b[T any]() *T {
	// data :=`<A[int]><name>name</name><data>1</data><errorResponse><age>10</age></errorResponse></A[int]>`
	data, err := xml.Marshal(&A[int]{Name: "name", Data: 1, ErrorResponse: ErrResp{Age: 10}})
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", string(data))
	a1 := A[T]{}
	err = xml.Unmarshal([]byte(data), &a1)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", a1)
	return &a1.Data
}

What did you expect to see?

1

What did you see instead?

<A[int]><name>name</name><data>1</data><errorResponse><age>10</age></errorResponse></A[int]>
panic: XML syntax error on line 1: expected attribute name in element

goroutine 1 [running]:
main.b[.shape.int](0x10f3f80)
	/Users/nzlov/workspace/vgo/generices/main.go:45 +0x1dd
main.main()
	/Users/nzlov/workspace/vgo/generices/main.go:21 +0x25
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions