LoginSignup
0
1

More than 3 years have passed since last update.

go/mysqlで値が上手く取れない

Posted at

go/gorm/mysql
で値が上手く取れない現象が起きたので備忘録がてら例えばこのようなテーブルを作成し、

CREATE TABLE dataids (
    id INT, 
    titleid INT,
    titleid2 INT
);

以下のように、insertしときます。

INSERT INTO collectq_db.dataids (id, titleid, titleid2) VALUES
(1,1,1),(2,1,2),(3,1,3);

そしてgormで取得しようと以下のようにしました

type Dataid struct {
    id       int
    titleid  int
    titleid2 int
}

func GetAllDataids(db *gorm.DB) []Dataid {
    var dataids []Dataid
    db.Find(&dataids)
    fmt.Println("確認!!", dataids)
    return dataids

とすると、以下のように、
fmt.Println上では
[{0 } {0 } {0 }]
googlechrome上では、
`[{},{},{}]
と表示されます。

解決策

構造体の中身の変数名さえ頭文字大文字にしなければ、反応gormのFindで
反応してくれない。
(errではなくよくわからないのが帰ってくる(まあ対応する変数がないので、代入されないだけだと思うけど))

type Dataid struct {
    Id       int
    Titleid  int
    Titleid2 int
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1