英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论