...

Source file src/github.com/pelletier/go-toml/v2/example_text_marshaling_test.go

Documentation: github.com/pelletier/go-toml/v2

     1  package toml_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  
     8  	"github.com/pelletier/go-toml/v2"
     9  )
    10  
    11  type customInt int
    12  
    13  func (i *customInt) UnmarshalText(b []byte) error {
    14  	x, err := strconv.ParseInt(string(b), 10, 32)
    15  	if err != nil {
    16  		return err
    17  	}
    18  	*i = customInt(x * 100)
    19  	return nil
    20  }
    21  
    22  type doc struct {
    23  	Value customInt
    24  }
    25  
    26  func ExampleUnmarshal_textUnmarshal() {
    27  	var x doc
    28  
    29  	data := []byte(`value  = "42"`)
    30  	err := toml.Unmarshal(data, &x)
    31  	if err != nil {
    32  		log.Fatal(err)
    33  	}
    34  	fmt.Println(x)
    35  	// Output:
    36  	// {4200}
    37  }
    38  

View as plain text