1

There have two examples:

  1. fun Marshal: You can see the example are not contain any button.

    source

    func ExampleMarshal() {
        type Item struct {
            Foo string
        }
    
        b, err := msgpack.Marshal(&Item{Foo: "bar"})
        if err != nil {
            panic(err)
        }
    
        var item Item
        err = msgpack.Unmarshal(b, &item)
        if err != nil {
            panic(err)
        }
        fmt.Println(item.Foo)
        // Output: bar
    }
    
  2. func Reverse well it has three buttons: Share, Format, Run

    source

    func ExampleReverse() {
        s := []int{5, 2, 6, 3, 1, 4} // unsorted
        sort.Sort(sort.Reverse(sort.IntSlice(s)))
        fmt.Println(s)
        // Output: [6 5 4 3 2 1]
    }
    

enter image description here

I don't understand why one has buttons, and the other doesn't? Does the content (godoc) rendered have anything to do with the version of Go used in the repository?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Carson
  • 6,105
  • 2
  • 37
  • 45
  • Those with the `Run` button are runnable, i.e. they have a `main`, and they are in `main` package. – Burak Serdar Jul 06 '22 at 04:37
  • But the [source](https://cs.opensource.google/go/go/+/refs/tags/go1.18.3:src/sort/example_test.go;l=63-68) does not have the `main` it seems that GoDoc automatically generates `main` based on some mechanism. – Carson Jul 06 '22 at 05:18
  • There is a `-play` flag to enable the playground in `godoc` command, but I suspect pkg.go.dev is built with additional steps and/or imports additional javascript to make examples runnable. – blackgreen Jul 06 '22 at 07:11
  • I thought at first that maybe it was limited to go source code, and not third parties. But there is runnable examples in https://github.com/pkg/errors as well. msgpack has its test in an `example_test.go` file with a `package msgpack_test` package and the correct function naming. Maybe it's a bug? – Dolanor Jul 06 '22 at 09:59

0 Answers0