how to define column type in golang type struct as longtext?

huangapple go评论91阅读模式
英文:

how to define column type in golang type struct as longtext?

问题

我有这段代码:

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

但是这只给我255个字符的varchar类型的Body字段。
我该如何将其设置为longtext类型?

这是martini框架示例应用程序中的代码。

英文:

I have this peace of code:

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

but that gets me only 255 varchar for Body.
How can i set that to be longtext?

This is from example app for martini framework.

答案1

得分: 0

在Go语言中,字符串的最大长度肯定比255要大得多。如果你看一下这段代码

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

你会发现字符串的输出和长度明显大于255。所以要么你将其保存到数据库中,数据库会对其进行截断,要么我宁愿创建一个漂亮的可重现的示例。

英文:

Maximum length of a string in go is definitely way bigger than 255. If you will look at 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))

you will see that the output of a string and the length is clearly bigger than 255. So either you are saving it to database, which truncates it, or I would rather create a nice reproducible example.

huangapple
  • 本文由 发表于 2015年5月29日 09:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/30519847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定