Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 我有一个用户表。我想写一个API,用id抓取数据。我一直得到下面的错误。请注意,数据库已经满了_Go_Golang Migrate - Fatal编程技术网

Go 我有一个用户表。我想写一个API,用id抓取数据。我一直得到下面的错误。请注意,数据库已经满了

Go 我有一个用户表。我想写一个API,用id抓取数据。我一直得到下面的错误。请注意,数据库已经满了,go,golang-migrate,Go,Golang Migrate,我有一个用户表。我想写一个API,用id抓取数据。我一直得到下面的错误。请注意,数据库已经满了。我想创建一个getapi,在其中传递id并获取该行作为结果。我试过一些方法,但我总是犯这个错误。 数据库模式- deadpool=# \d user_data Table "public.user_data" Column | Type | Colla

我有一个用户表。我想写一个API,用id抓取数据。我一直得到下面的错误。请注意,数据库已经满了。我想创建一个getapi,在其中传递id并获取该行作为结果。我试过一些方法,但我总是犯这个错误。 数据库模式-

deadpool=# \d user_data
                                           Table "public.user_data"
       Column       |           Type           | Collation | Nullable |                Default
--------------------+--------------------------+-----------+----------+---------------------------------------
 id                 | integer                  |           | not null | nextval('user_data_id_seq'::regclass)
 created_at         | timestamp with time zone |           |          |
 updated_at         | timestamp with time zone |           |          |
 deleted_at         | timestamp with time zone |           |          |
 name               | text                     |           |          |
 age                | text                     |           |          |
 gender             | text                     |           |          |
 party_code         | text                     |           |          |
 criminal_cases     | text                     |           |          |
 number_of_cases    | text                     |           |          |
 serious_ipc_counts | text                     |           |          |
 ipc_details        | text                     |           |          |
 education_level    | text                     |           |          |
 movable_assets     | text                     |           |          |
 immovable_assets   | text                     |           |          |
 total_assets       | text                     |           |          |
 total_liabilities  | text                     |           |          |
 pan_given          | text                     |           |          |
 election           | text                     |           |          |
 constituency       | text                     |           |          |
Indexes:
    "user_data_pkey" PRIMARY KEY, btree (id)
    "idx_user_data_deleted_at" btree (deleted_at)
我的代码- 包干管

import (
    "encoding/json"
    "fmt"
    "github.com/gorilla/mux"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
    "log"
    "net/http"
)
var db *gorm.DB
var err error
type UserData struct {
    gorm.Model
    Name               string `json:"name"`
    Age                string `json:"age"`
    Gender             string `json:"gender"`
    Party_Code         string `json:"party_code"`
    Criminal_Cases     string `json:"criminal_cases"`
    Number_of_Cases    string `json:"number_of_cases"`
    Serious_IPC_Counts string `json:"serious_ipc_counts"`
    IPC_Details        string `json:"ipc_details"`
    Education_Level    string `json:"education_level"`
    Movable_Assets     string `json:"movable_assets"`
    Immovable_Assets   string `json:"immovable_assets"`
    Total_Assets       string `json:"total_assets"`
    Total_Liabilities  string `json:"total_liabilities"`
    PAN_Given          string `json:"pan_given"`
    Election           string `json:"election"`
    Constituency       string `json:"constituency"`
}
func main() {
    // NOTE: See we’re using = to assign the global var
    // instead of := which would assign it only in this function
    db, err := gorm.Open("postgres", "host=localhost port=5432 dbname=deadpool sslmode=disable")
    if err != nil {
        fmt.Println(err)
    }
    defer db.Close()
    router := mux.NewRouter()
    router.HandleFunc("/resources/{id}", GetResource).Methods("GET")
    log.Fatal(http.ListenAndServe(":8080", router))
}

func GetResource(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)
    var userData UserData
    db.First(&userData, params["id"])
    json.NewEncoder(w).Encode(&userData)
}
我面临的错误-

> 2019/06/13 18:41:22 http: panic serving [::1]:56779: runtime error:
> invalid memory address or nil pointer dereference goroutine 47
> [running]: net/http.(*conn).serve.func1(0xc00020e460)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1746 +0xd0
> panic(0x13ba240, 0x1714f30)
>         /usr/local/Cellar/go/1.11.4/libexec/src/runtime/panic.go:513 +0x1b9 github.com/jinzhu/gorm.(*DB).clone(0x0, 0x10)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:768 +0x26 github.com/jinzhu/gorm.(*DB).NewScope(0x0, 0x1384740, 0xc000244500, 0x0)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:179 +0x2f github.com/jinzhu/gorm.(*DB).First(0x0, 0x1384740, 0xc000244500, 0xc00018a830, 0x1, 0x1, 0x12cdeda)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:299 +0x5a main.GetResource(0x1490560, 0xc000240380, 0xc000189600)
>         /Users/abhisekroy/go/src/github.com/CreditSaisonIndia/deadpool/ExternalVerifications/externalVerifications.go:49
> +0x13d net/http.HandlerFunc.ServeHTTP(0x143ba08, 0x1490560, 0xc000240380, 0xc000189600)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1964 +0x44
> github.com/gorilla/mux.(*Router).ServeHTTP(0xc0000ec480, 0x1490560,
> 0xc000240380, 0xc00022b200)
>         /Users/abhisekroy/go/src/github.com/gorilla/mux/mux.go:212 +0xd0 net/http.serverHandler.ServeHTTP(0xc000087520, 0x1490560, 0xc000240380, 0xc00022b200)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:2741 +0xab
> net/http.(*conn).serve(0xc00020e460, 0x14908a0, 0xc000224880)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1847 +0x646
> created by net/http.(*Server).Serve
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:2851 +0x2f5
> 2019/06/13 18:41:22 http: panic serving [::1]:56780: runtime error:
> invalid memory address or nil pointer dereference goroutine 48
> [running]: net/http.(*conn).serve.func1(0xc00020e500)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1746 +0xd0
> panic(0x13ba240, 0x1714f30)
>         /usr/local/Cellar/go/1.11.4/libexec/src/runtime/panic.go:513 +0x1b9 github.com/jinzhu/gorm.(*DB).clone(0x0, 0x10)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:768 +0x26 github.com/jinzhu/gorm.(*DB).NewScope(0x0, 0x1384740, 0xc00026c780, 0x0)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:179 +0x2f github.com/jinzhu/gorm.(*DB).First(0x0, 0x1384740, 0xc00026c780, 0xc000226250, 0x1, 0x1, 0x12cdeda)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:299 +0x5a main.GetResource(0x1490560, 0xc000266540, 0xc00022b600)
>         /Users/abhisekroy/go/src/github.com/CreditSaisonIndia/deadpool/ExternalVerifications/externalVerifications.go:49
> +0x13d net/http.HandlerFunc.ServeHTTP(0x143ba08, 0x1490560, 0xc000266540, 0xc00022b600)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1964 +0x44
> github.com/gorilla/mux.(*Router).ServeHTTP(0xc0000ec480, 0x1490560,
> 0xc000266540, 0xc00022b400)
>         /Users/abhisekroy/go/src/github.com/gorilla/mux/mux.go:212 +0xd0 net/http.serverHandler.ServeHTTP(0xc000087520, 0x1490560, 0xc000266540, 0xc00022b400)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:2741 +0xab
> net/http.(*conn).serve(0xc00020e500, 0x14908a0, 0xc000224980)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1847 +0x646
> created by net/http.(*Server).Serve
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:2851 +0x2f5

您没有设置包级别
db
var,因为您使用了
:=

如果出现错误,请退出
main
func,这样就不会继续使用nil db

var db *gorm.DB

func main() {
    var err error
    db, err = gorm.Open()
    if err != nil {
        log.Fatalf("unable to connect to database: %s", err.Error())
    }
}

main
中,您检查错误,如果有错误,您可以打印它,但您可以继续,就好像错误从未发生过一样。您不能这样做-错误意味着调用失败,不应使用随错误一起返回的值。另外,请指出第47行上的代码。