Search code examples
gomartini

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


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.


Solution

  • 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.