Your function appears to return error text, but it will be much more clear 
what's going on when reading the code if you encode your intent in the return 
type and return an error instead of a string (which gives me no information 
about what that string is supposed to represent: is it an error? is it some 
XML?). fmt.Errorf can be used for this, also just return the io.EOF directly.

—Sam

On Sun, Feb 24, 2019, at 17:28, Steffen Wentzel wrote:
> Thanks for the response. I was hoping it would be possible with just 
> xml.Unmarshal.
> 
> I now have this implementation - any comments on that? (I'm okay with 
> returning the error inline, it's just for logging purposes):
> 
> Playground: https://play.golang.org/p/6J8XndWdlv_N
> 
> `
> func id2(axl []byte) string {
>  dec := xml.NewDecoder(bytes.NewReader(axl))
>  for {
>  tok, err := dec.Token()
>  if err != nil {
>  if err == io.EOF {
>  return "token not found"
>  }
>  return fmt.Sprintf("error decoding XML: %v", err)
> 
>  }
>  if tok, ok := tok.(xml.StartElement); ok {
>  if tok.Name.Local == "return" {
>  var id string
>  err := dec.DecodeElement(&id, &tok)
>  if err != nil {
>  return fmt.Sprintf("error decoding return element: %v", err)
>  }
>  return id
>  }
>  }
>  }
> }
> `
> 
> 
> 
> Am Samstag, 23. Februar 2019 20:53:07 UTC+1 schrieb Tamás Gulácsi:Walk 
> over it usin xml.Decoder.Token, and UnmarshalElement when found a 
> StartToken with a proper Name.Local.
>  
> 
> 
>  -- 
>  You received this message because you are subscribed to the Google 
> Groups "golang-nuts" group.
>  To unsubscribe from this group and stop receiving emails from it, send 
> an email to golang-nuts+unsubscr...@googlegroups.com.
>  For more options, visit https://groups.google.com/d/optout.
>

-- 
Sam Whited

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to