I'm new to Go and I'm trying to write a little program to save enumerated values to a database. The way I declare my values is as follows:
type FileType int64
const (
movie FileType = iota
music
book
etc
)
I use these values in my struct like this:
type File struct {
Name string
Type FileType
Size int64
}
I use gorp for my database stuff, but I guess the use of gorp isn't relevant to my problem. I put stuff in my DB like this:
dbmap.Insert(&File{"MyBook.pdf",movie,1000})
but when I try to retrieve stuff…
dbmap.Select(&dbFiles, "select * from Files")
I get the following error:
panic: reflect.Set: value of type int64 is not assignable to type main.FileType
When I use int64
as the type for the const(...)
and for the File.Type
field, everything works fine, but I'm new to Go and want to understand the problem.
The way I see it, I have two problems:
- Why can't Go convert this stuff successfully? I looked at the source code of the Go reflection and sql packages and there are methods for this kind of conversion, but they seem to fail. Is this a bug? What is the problem?
-
I figured out, that one can implement the
sql.Scanner
interface by implementing the following method:Scan(src interface{}) error
I tried to implement the method and I even was able to get the right value from
src
and convert it to aFileType
, but I was confused if I should implement the method for "(f *FileType)
or(f FileType)
. Either way the method gets invoked, however I'm not able to overwritef
(or at least the update gets lost later) and theFile
instances read from the DB always had a "0" as value forFile.Type
.
Do you have any ideas on those two points?