【问题标题】:how to define column type in golang type struct as longtext?如何将golang类型结构中的列类型定义为longtext?
【发布时间】:2015-05-29 01:48:22
【问题描述】:

我有这样的代码和平:

type Post struct {
    Id      int64 `db:"post_id"`
    Created int64
    Title   string `form:"Title"`
    Body    string `form:"Body" binding:"required"`

}

但这让我只有 255 个 varchar 用于 Body。 如何将其设置为长文本?

这是来自 martini 框架的示例应用程序。

【问题讨论】:

    标签: go martini


    【解决方案1】:

    go 中字符串的最大长度肯定大于 255。如果你看看this code

    myPost := Post{
      Id: 43,
      Created: 324,
      Title: "title",
      Body: "very long string",
    }
    fmt.Println(myPost.Body)
    fmt.Println()
    fmt.Println(len(myPost.Body))
    

    你会看到一个字符串的输出和长度明显大于 255。所以要么你将它保存到数据库中,它会截断它,或者我宁愿创建一个很好的可重现示例。

    【讨论】:

    • 所以我无法使用 dbmap.AddTableWithName(Post{}, "posts").SetKeys(true, "Id") 并拥有长文本?我必须先定义数据库表?
    猜你喜欢
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    相关资源
    最近更新 更多