What input will cause golang's json.Marshal to return an error?

后端 未结 4 1367
栀梦
栀梦 2020-12-09 14:29

From the docs:

JSON cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an i

4条回答
  •  旧巷少年郎
    2020-12-09 15:05

    Update: now using a channel instead of a map[int]int to elicit the error


    Go-specific structures,e.g. func or chan refuse to serialize:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        value := make(chan int)
        _, err := json.Marshal(value)
        fmt.Println(err)
    }
    

提交回复
热议问题