Go语言 通过http请求追加到现有列表

zvokhttg  于 2023-03-06  发布在  Go
关注(0)|答案(1)|浏览(108)

我正在使用Echo制作一个简单的REST API,我有一个变量,它是下面的Map,基于我制作的这个结构体:

type Checklist struct {
        ID         int      `json:"id"`
        Title      string   `json:"title"`
        Lines      []string `json:"lines"`
        AuthorName string   `json:"authorName"`
        AuthorID   int      `json:"authorID"`
        Tags       []Tag    `json:"tags"`
    }

    var (
        checklists    = map[int]*Checklist{}
        checklistSeq  = 1
        checklistLock = sync.Mutex{}
    )

创建新的检查表并将其附加到checklists变量之后,如何发送在新检查表的Lines字段中附加新行的请求?
我想出的解决办法是这样的:

func createChecklist(c echo.Context) error {
        checklistLock.Lock()
        defer checklistLock.Unlock()
        newChecklist := &Checklist{
            ID:    checklistSeq,
            Lines: make([]string, 0),
            Tags:  make([]Tag, 0),
        }
        if err := c.Bind(newChecklist); err != nil {
            return err
        }
        checklists[newChecklist.ID] = newChecklist
        checklistSeq++
        return c.JSON(http.StatusOK, newChecklist)
    }
    func addLine(c echo.Context) error {
        checklistLock.Lock()
        defer checklistLock.Unlock()
        id, _ := strconv.Atoi(c.Param("id"))
        checklist := *checklists[id]
        line := []string{""}
        if err := c.Bind(line); err != nil {
            return err
        }
        checklist.Lines = line
        return c.JSON(http.StatusCreated, checklists)
    }

但是,当我测试这个处理程序时,它给出了以下结果:

// 1: Creating a new checklist

    $ curl -X POST -H 'Content-Type: application/json' -d '{"title": "test"}' localhost:1234/checklist

    >> {"id":1,"title":"test","lines":[],"authorName":"","authorID":0,"tags":[]}

    // 2: Check to see the checklist has been created.
 
    $ curl -X GET localhost:1234/checklist

    >> {"1":{"id":1,"title":"test","lines":[],"authorName":"","authorID":0,"tags":[]}}
    // 3: Attempting to create a new line
    $ curl -X POST -H 'Content-Type: application/json' -d '{"lines": "test123"}' localhost:1234/checklist/1

    >> curl: (52) Empty reply from server

    // 4: Confirming it hasn't been created.

    $ curl -X GET localhost:1234/checklist

    >> {"1":{"id":1,"title":"test","lines":[],"authorName":"","authorID":0,"tags":[]}}

因此,该函数实际上并不起作用,因为将POST ping到适当的路由时,既没有返回预期的响应,也没有将行实际添加到字段中。

f0brbegy

f0brbegy1#

func addLine(c echo.Context) error {
    checklistLock.Lock()
    defer checklistLock.Unlock()

    id, err := strconv.Atoi(c.Param("id"))
    if err != nil {
        return err
    }

    // do not deref *Checklist
    cl, ok := checklists[id]
    if !ok {
        return echo.ErrNotFound
    }

    // use same structure as the expected JSON
    var input struct { Lines []string `json:"lines"` }

    // always pass a pointer to c.Bind
    if err := c.Bind(&input); err != nil {
        return err
    }

    // do not overwrite previously written lines, use append
    cl.Lines = append(cl.Lines, input.Lines...)

    return c.JSON(http.StatusOK, cl)
}

现在试试:

$ curl -X POST -H 'Content-Type: application/json' -d '{"lines": ["test123"]}' localhost:1234/checklist/1
  • (注意"test123"括在括号中)*

相关问题