golang Gorp SELECT 出现错误

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

golang Gorp error with SELECT

问题

我正在尝试使用GORP从MySQL数据库中进行SELECT查询。
我遇到了一个错误,错误信息是**"reflect.Value.Interface: cannot return value obtained from unexported field or method"**。
我已经验证了数据库的连接性。例如,Select (*) count给出了正确的计数。我发现它在以下代码行上失败:

dbmap.Select(&dd, "SELECT * FROM kd_dropdowns")

如果没有上述行,程序不会抛出任何错误。

以下是我的代码。

package main
import (
	"database/sql"
	"fmt"
	"log"
	"net/http"

	"github.com/coopernurse/gorp"
	_ "github.com/go-sql-driver/mysql"
	"github.com/gorilla/mux"
)

func main() {

	fmt.Println("reached main")
	// 创建一个MUX
	r := mux.NewRouter()

	//manegala patti
	r.HandleFunc("/manegalu", manegalaTorisu).Methods("GET")

	http.ListenAndServe(":8080", r)

}
func manegalaTorisu(w http.ResponseWriter, r *http.Request) {
	type Dropdowns struct {
		dd_id      int64  `db:"dd_id"`
		identifier int64  `db:"identifier"`
		name       string `db:"name"`
		active     string `db:"active"`
	}

	db, err := sql.Open("mysql", "krishna:xxxx@/kd")
	defer db.Close()

	var dbmap *gorp.DbMap
	// 构建一个gorp DbMap
	dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{}}

	var dd []Dropdowns
	_, err = dbmap.Select(&dd, "SELECT * FROM kd_dropdowns")
	checkErr(err, "Select failed")
	fmt.Fprint(w, "Success")
}

func checkErr(err error, msg string) {
	if err != nil {
		log.Fatalln(msg, err)
	}
}

![这是我的MySQL中的表格样式][1]
[1]: http://i.stack.imgur.com/HLrqJ.jpg

英文:

I am trying to do a SELECT from mySQL database with GORP.
I am getting an error which says "reflect.Value.Interface: cannot return value obtained from unexported field or method".
I have verified the DB connectivity. For example Select (*) count gives the right count. I see that it fails on

> dbmap.Select(&dd, "SELECT * FROM kd_dropdowns")

Without the above line program does not throw any error.

Here is my code.

package main
import (
"database/sql"
"fmt"
"log"
"net/http"

"github.com/coopernurse/gorp"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)

func main() {

fmt.Println("reached main")
// Create a MUX
r := mux.NewRouter()

//manegala patti
r.HandleFunc("/manegalu", manegalaTorisu).Methods("GET")

http.ListenAndServe(":8080", r)

}
func manegalaTorisu(w http.ResponseWriter, r *http.Request) {
type Dropdowns struct {
	dd_id      int64  `db:"dd_id"`
	identifier int64  `db:"identifier"`
	name       string `db:"name"`
	active     string `db:"active"`
}

db, err := sql.Open("mysql", "krishna:xxxx@/kd")
defer db.Close()

var dbmap *gorp.DbMap
// construct a gorp DbMap
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{}}

var dd []Dropdowns
_, err = dbmap.Select(&dd, "SELECT * FROM kd_dropdowns")
checkErr(err, "Select failed")
fmt.Fprint(w, "Success")
}

func checkErr(err error, msg string) {
if err != nil {
	log.Fatalln(msg, err)
}
}

![This is how table looks in mySQL][1]
[1]: http://i.stack.imgur.com/HLrqJ.jpg

答案1

得分: 4

这是Go语言初学者常见的陷阱。

结构体中的所有字段都根据首字母是大写还是小写来确定是否可导出隐藏

Gorp试图访问你的结构体中的可导出字段。但是你使用了小写字母作为首字母,所以这些字段是隐藏的,因此访问失败。

尝试使用以下代码:

...
type Dropdowns struct {
    DdId       int64  `db:"dd_id"`
    Identifier int64  `db:"identifier"`
    Name       string `db:"name"`
    Active     string `db:"active"`
}
...

如果你喜欢,你可以将dd_id改为驼峰命名法,即DdId(我认为这在Go语言中更符合惯例)。

请注意,Go语言中的大写导出特性适用于常量、包变量、类型和函数名,以及结构体中的字段。

英文:

This is a common trap for people starting with Go.

All the fields in a struct are exported or hidden simply based on the first letter: if it is uppercase, the field is exported. Otherwise, it is not.

Gorp is trying to access the exported fields in your struct. But you've used lowercase first-letters, so the fields are hidden and so it fails.

Try this instead.

...
type Dropdowns struct {
    DdId       int64  `db:"dd_id"`
    Identifier int64  `db:"identifier"`
    Name       string `db:"name"`
    Active     string `db:"active"`
}
...

You can camelcase your dd_id as DdId if you prefer (I think this is more idiomatic in Go).

Note that the uppercase export feature of Go applies to constants, package variables, types and function names, as well as the fields within structs.

huangapple
  • 本文由 发表于 2014年11月24日 06:48:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/27095344.html
匿名

发表评论

匿名网友

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

确定