๋๋ ๋ค์์ ๊ฐ์ง๊ณ ์์ต๋๋ค :
type BufferType string
const (
Console BufferType = "console"
Channel BufferType = "channel"
Conversation BufferType = "conversation"
)
type Buffer struct {
Bid int64 `gorm:"primary_key:yes"`
Type BufferType `sql:"not null;type:ENUM('console', 'channel', 'conversation')"`
Name string `sql:"size:255;not null"`
}
db.Save(buffer)๋ฅผ ์๋ํ๋ฉด ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
sql: converting Exec argument #1's type: unsupported type BufferType, a string)
BufferType์ด ์ค์ ๋ก ๋ฌธ์์ด์ผ ๋ฟ์์ ํด๊ฒฐํ๊ณ ์ ์ฅํ ์ ์์ต๋๊น? ์ด ๊ตฌ์กฐ์ฒด๋ฅผ db.Find()๋ก ๋ก๋ํ๋ ค๊ณ ํ๋ฉด ๋น์ทํ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
๋๋ฒ๊น ์ ์ข ๋ ํด๋ณด๋ ์ด ์ค๋ฅ๊ฐ golang ๋ฐ์ดํฐ๋ฒ ์ด์ค/sql ์ฝ๋์์ ๋์ค๋ ๊ฒ ๊ฐ์๋ฐ ์ฌ๊ธฐ ์์ ํด๊ฒฐ์ฑ ์ ์ฐพ์
func (u *BufferType) Scan(value interface{}) error { *u = BufferType(value.(string)); return nil }
func (u BufferType) Value() (driver.Value, error) { return string(u), nil }
์ฝ๊ฐ ํฌ๋ฐํ์ง๋ง ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ๋งคํํ๋ ๋ฐฉ๋ฒ์ ์๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค. ํ์ํ ๊ฒฝ์ฐ ์ฌ๊ธฐ์ ์ค๋ฅ ๊ฒ์ฌ๋ฅผ ์ถ๊ฐํ ์๋ ์์ต๋๋ค.
๋๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์๋ค. @bramp ์ ์๋ฃจ์ ์ ์ฌ์ ํ โโ๋์๊ฒ ์ค๋ฅ๋ฅผ ์ฃผ์๋ค. ๋ค์์ ๋๋ฅผ ์ํด ์ผํ์ต๋๋ค.
func (u *BufferType) Scan(value interface{}) error { *u = BufferType(value.([]byte)); return nil }
func (u BufferType) Value() (driver.Value, error) { return string(u), nil }
์ฐจ์ด๋ ์ฌ์ฉ []byte
๋์ string
. ์ด stackoverflow ์ค๋ ๋๊ฐ ์ ๋ฅผ ๋์์ต๋๋ค: http://stackoverflow.com/a/20582504/2418739
+1 ํ๋ฅญํ ์๋ฃจ์ . ์ด๊ฒ์ด GORM ๋ฌธ์์ ์์์ผ๋ฉด ํฉ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์๋ค. @bramp ์ ์๋ฃจ์ ์ ์ฌ์ ํ โโ๋์๊ฒ ์ค๋ฅ๋ฅผ ์ฃผ์๋ค. ๋ค์์ ๋๋ฅผ ์ํด ์ผํ์ต๋๋ค.
์ฐจ์ด๋ ์ฌ์ฉ
[]byte
๋์string
. ์ด stackoverflow ์ค๋ ๋๊ฐ ์ ๋ฅผ ๋์์ต๋๋ค: http://stackoverflow.com/a/20582504/2418739