douna3367 2019-08-22 06:35
浏览 42

错误; 不能使用图像名称(图像名称类型)作为dao。附录中的图像名称

I'm refactoring server side logic in Go, but there is error that I can't resolve.

・client: react/axios
・api: golang/gin
・web-server: nginx
・db: mysql
・container: docker
・ci-tool: travis
・deploy: aws elastic beanstalk

service.go

func GetSingleArticleService(c *gin.Context, db *sql.DB) Article {

    article, rows := dao.GetSingleArticleDao(c, db)
    for rows.Next() {
        imageName := ImageName{}
        err := rows.Scan(&imageName.NAME)
        if err != nil {
            panic(err.Error())
        }
        // error occurs here
        article.IMAGENAMES = append(article.IMAGENAMES, imageName)
    }

    return article
}

dao.go

func GetSingleArticleDao(c *gin.Context, db *sql.DB) (Article, *sql.Rows) {
    id := c.Params.ByName("id")
    article := Article{}
    errArticle := db.QueryRow("SELECT * FROM articles WHERE id = ?", id).Scan(&article.ID, &article.UUID, &article.TITLE, &article.CONTENT)
    if errArticle != nil {
        panic(errArticle.Error())
    }
    rows, errImage := db.Query("SELECT image_name FROM images WHERE article_uuid  = ?", article.UUID)
    if errImage != nil {
        panic(errImage.Error())
    }

    return article, rows
}

I expect there is no compile error, but the actual is not. There is error at service.go.

cannot use imageName (type ImageName) as type dao.ImageName in append
  • 写回答

1条回答 默认 最新

  • donqo88682 2019-08-22 07:41
    关注

    As suggested by @ Volker, I make util.go file for common model and import it.

    package util
    
    type Article struct {
        ID         int         `json:"id"`
        UUID       string      `json:"uuid"`
        TITLE      string      `json:"title"`
        CONTENT    string      `json:"content"`
        IMAGENAMES []ImageName `json:"imageNames"`
    }
    
    type ImageName struct {
        NAME string `json:"name"`
    }
    
    type ImageData struct {
        ARTICLEUUID string      `json:"articleUUID"`
        IMAGENAMES  []ImageName `json:"imageNames"`
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?