我正在尝试学习Go并尝试将行扫描到值,然后我将分配给结构。似乎并非所有的值都进入结构但我不知道为什么。下面是完整的脚本。对服务器运行查询实际上返回结果。但是,当我在扫描结果中进行测试时,不会显示任何数据。 在这种情况下,预期结果将是 “恩” “恩” 下面是脚本。我很确定我的结构或我的类型(或两者)都搞乱了。
package main
import (
"log"
_ "github.com/lib/pq"
"fmt"
"database/sql"
)
//local variables
var cnstr = "cnstr"
var dbdriver = "postgres"
//data structure
type WordData struct {
Word string
WordID int
Root string
RootID int
PartOfSpeech string
Case *string
Person *string
Gender *string
Number *string
Tense *string
Voice *string
Mood *string
Transliteration *string
Root_Definition *string
}
func main() {
db, _ := sql.Open(dbdriver, cnstr)
var word string
var wordid int
var root string
var rootid int
var partofspeech string
var caseval *string
var person *string
var gender *string
var number *string
var tense *string
var voice *string
var mood *string
var transliteration *string
var definition *string
if db != nil {
err := db.QueryRow(`select
morph as word,
morph_id as wordid,
lemma as root,
lemma_id as rootid,
part_of_speech as partofspeech,
"case",
person, gender, number, tense, voice, mood,
transliteration,
definition
from texts where sort_order = 65006`).Scan(&word,&wordid,&root,&rootid,&partofspeech,&caseval,&person,&gender,&number,&tense,&voice,&mood,&transliteration,&definition)
switch {
case err == sql.ErrNoRows:
log.Printf("No user with that ID.")
case err != nil:
log.Fatal(err)
default:
/*wd := new (WordData)
wd.Word = word
wd.WordID = wordid
wd.Root = root
wd.RootID = rootid
wd.PartOfSpeech = partofspeech
wd.Case = caseval
wd.Person = person
wd.Gender = gender
wd.Number = number
wd.Tense = tense
wd.Voice = voice
wd.Mood = mood
wd.Transliteration = transliteration
wd.Root_Definition = definition */
wd := WordData{
word,
wordid,
root,
rootid,
partofspeech,
caseval,
person,
gender,
number,
tense,
voice,
mood,
transliteration,
definition}
fmt.Printf("Printf: %s",wd.Transliteration)
fmt.Println(transliteration)
}
//or, comment out and use the other initializer type
}
db.Close()
}