有没有更简单的方法来附加结构片段?

The following code works, but I want to find a simpler way to do it

package main

import "fmt"

type steps []struct {
    i int
    j int
}

func main() {
    steps := steps{}
    type step struct{ i, j int }
    steps = append(steps, step{1, 1}, step{1, 2})
    fmt.Println(steps)
}

Specifically, I don't want to define a new type just so I can append it to a slice. For example, I want to do it like this:

package main

import "fmt"

type steps []struct {
    i int
    j int
}

func main() {
    steps := steps{}
    steps = append(steps, {1, 1}, {1, 2}) // syntax error
    fmt.Println(steps)
}

But I'll get a "syntax error: unexpected {, expecting expression"

I don't understand why I can't do it this way, the data structure is correct.

You created an anonymous struct in your slice, so you need to repeat the schema when adding elements:

// works - but a bit tedious...
steps = append(steps,
        struct {
            i int
            j int
        }{1, 1},
        struct {
            i int
            j int
        }{1, 2},
)

or define the sub-type:

type step struct {
    i int
    j int
}
type steps []step

steps = append(steps, step{3, 4}, step{5, 6})

playground example