github.com/enetx/g@v1.0.80/examples/xml.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  
     7  	"github.com/enetx/g"
     8  )
     9  
    10  func main() {
    11  	type Plant struct {
    12  		XMLName xml.Name        `xml:"plant"`
    13  		ID      g.Int           `xml:"id,attr"`
    14  		Name    g.String        `xml:"name"`
    15  		Origin  g.Slice[string] `xml:"origin"`
    16  	}
    17  
    18  	coffee := &Plant{ID: 27, Name: "Coffee"}
    19  	coffee.Origin = g.SliceOf("Ethiopia", "Brazil")
    20  
    21  	s := g.NewString("").Enc().XML(coffee, "", "  ").Unwrap().Append("\n")
    22  	fmt.Println(s)
    23  
    24  	var coffee2 Plant
    25  
    26  	s.Dec().XML(&coffee2)
    27  	fmt.Println(coffee2.Origin.Get(-1))
    28  }