github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/testdata/json.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  )
     6  
     7  func main() {
     8  	println("int:", encode(3))
     9  	println("float64:", encode(3.14))
    10  	println("string:", encode("foo"))
    11  	println("slice of strings:", encode([]string{"foo", "bar"}))
    12  }
    13  
    14  func encode(itf interface{}) string {
    15  	buf, err := json.Marshal(itf)
    16  	if err != nil {
    17  		panic("failed to JSON encode: " + err.Error())
    18  	}
    19  	return string(buf)
    20  }