{"id":42780,"date":"2024-12-01T12:06:26","date_gmt":"2024-12-01T04:06:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42780\/"},"modified":"2024-12-01T12:06:26","modified_gmt":"2024-12-01T04:06:26","slug":"%e8%bf%90%e8%a1%8c%e6%97%b6%e9%94%99%e8%af%af%ef%bc%9a%e6%97%a0%e6%95%88%e7%9a%84%e5%86%85%e5%ad%98%e5%9c%b0%e5%9d%80%e6%88%96-nil-%e6%8c%87%e9%92%88%e5%8f%96%e6%b6%88%e5%bc%95%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42780\/","title":{"rendered":"\u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528 (2)"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528 (2)<\/span><\/p>\n<h1>\u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528 (2)<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 23:03:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528 (2)\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u662f <strong>go<\/strong> \u65b0\u624b\uff0c\u9700\u8981\u5e2e\u52a9\uff01<\/p>\n<p>\u5728 postgresql \u6570\u636e\u5e93\u4e2d\uff0c\u6211\u6709\u4e00\u4e2a\u540d\u4e3a <code>factors<\/code> \u7684\u8868\u3002\u8be5\u8868\u6709 2 \u5217\uff08<code>factor_id<\/code> \u548c <code>factor_name<\/code>\uff09\u3002\u73b0\u5728\u6211\u5df2\u6210\u529f\u4ece go \u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u5230 postgresql \u6570\u636e\u5e93\u3002<\/p>\n<p>\u5f53\u6211\u5c1d\u8bd5\u5728\u63a7\u5236\u53f0\u4e2d\u53d1\u51fa\u57fa\u4e8e\u67e5\u8be2\u7684 get \u8bf7\u6c42\u65f6\uff0c\u6211\u770b\u5230\u9519\u8bef\u3002<\/p>\n<pre>http:\/\/localhost:8000\/api\/factors\/?limit=5&amp;offset=1<\/pre>\n<p>\u6211\u54ea\u91cc\u51fa\u9519\u4e86\uff1f\u8bf7\u5e2e\u6211\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p><strong>controllers\/factors.go<\/strong>\uff1a<\/p>\n<pre>package controllers\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"net\/http\"\n    \"restify\/models\"\n    \"restify\/utils\"\n)\n\nvar getfactors = func(res http.responsewriter, req *http.request) {\n    data := models.factors{}\n\n    err := models.getfactors(&amp;data, req.url.query().get(\"limit\"), req.url.query().get(\"offset\"))\n    if err != nil {\n        fmt.println(err.error())\n        http.error(res, err.error(), 500)\n        return\n    }\n\n    out, err := json.marshal(data)\n    if err != nil {\n        fmt.println(err.error())\n        http.error(res, err.error(), 501)\n        return\n    }\n\n    resp := make(map[string]interface{})\n\n    resp[\"dictionaries\"] = string(out)\n\n    utils.respond(res, resp)\n\n}<\/pre>\n<p><strong>models\/factors.go<\/strong>\uff1a<\/p>\n<pre>package models\n\nimport (\n    \"fmt\"\n    \"restify\/database\"\n)\n\ntype factor struct {\n    factorid int `json:\"factor_id\"`\n    factorname string `json:\"factor_name\"`\n}\n\ntype factors struct {\n    array[]factor\n}\n\nfunc getfactors(data *factors, limit string, offset string) error {\n    rows, err := database.db.query(`select * from factors order by factor_id limit ` + limit + ` offset ` + offset + `;`)\n    if err != nil {\n        fmt.println(err)\n        return err\n    }\n    defer rows.close()\n    for rows.next() {\n        var id int\n        var name string\n        model := factor{}\n        err = rows.scan(&amp;id, &amp;name)\n        if err != nil {\n            fmt.println(err)\n            return err\n        }\n        model.factorid = id\n        model.factorname = name\n        data.array = append(data.array, model)\n    }\n    err = rows.err()\n    if err != nil {\n        return err\n    }\n    return nil\n}<\/pre>\n<p><strong>\u9519\u8bef\uff1a<\/strong><\/p>\n<pre>2019\/02\/26 16:25:55 http: panic serving [::1]:20932: runtime error: invalid memory address or nil pointer dereference\ngoroutine 11 [running]:\nnet\/http.(*conn).serve.func1(0xc0002badc0)\n    c:\/go\/src\/net\/http\/server.go:1746 +0xd7\npanic(0x6dad20, 0x970890)\n    c:\/go\/src\/runtime\/panic.go:513 +0x1c7\ndatabase\/sql.(*db).conn(0x0, 0x78a500, 0xc0000100a8, 0x10000c000339801, 0x0, 0xc000339898, 0xc000339830)\n    c:\/go\/src\/database\/sql\/sql.go:1081 +0x41\ndatabase\/sql.(*db).query(0x0, 0x78a500, 0xc0000100a8, 0xc000356040, 0x3a, 0x0, 0x0, 0x0, 0xc000356001, 0x3a, ...)\n    c:\/go\/src\/database\/sql\/sql.go:1514 +0x6d\ndatabase\/sql.(*db).querycontext(0x0, 0x78a500, 0xc0000100a8, 0xc000356040, 0x3a, 0x0, 0x0, 0x0, 0x5, 0xc000356040, ...)\n    c:\/go\/src\/database\/sql\/sql.go:1496 +0xda\ndatabase\/sql.(*db).query(0x0, 0xc000356040, 0x3a, 0x0, 0x0, 0x0, 0x8, 0xc00034e016, 0x1)\n    c:\/go\/src\/database\/sql\/sql.go:1510 +0x89\nrestify\/models.getfactors(0xc000339b28, 0xc00034e016, 0x1, 0xc00034e01e, 0x1, 0x0, 0x0)\n    c:\/users\/nnogerbek\/go\/src\/restify\/models\/factors.go:18 +0xf6\nrestify\/controllers.glob..func1(0x78a2c0, 0xc00035a000, 0xc000332200)\n    c:\/users\/nnogerbek\/go\/src\/restify\/controllers\/factors.go:14 +0xd3\nnet\/http.handlerfunc.servehttp(0x74da58, 0x78a2c0, 0xc00035a000, 0xc000332200)\n    c:\/go\/src\/net\/http\/server.go:1964 +0x4b\ngithub.com\/gorilla\/mux.(*router).servehttp(0xc0002fc180, 0x78a2c0, 0xc00035a000, 0xc000310300)\n    c:\/users\/nnogerbek\/go\/src\/github.com\/gorilla\/mux\/mux.go:212 +0xd7\ngithub.com\/gorilla\/handlers.(*cors).servehttp(0xc0001f2120, 0x78a2c0, 0xc00035a000, 0xc000310300)\n    c:\/users\/nnogerbek\/go\/src\/github.com\/gorilla\/handlers\/cors.go:54 +0xa95\nnet\/http.serverhandler.servehttp(0xc000056b60, 0x78a2c0, 0xc00035a000, 0xc000310300)\n    c:\/go\/src\/net\/http\/server.go:2741 +0xb2\nnet\/http.(*conn).serve(0xc0002badc0, 0x78a4c0, 0xc000340040)\n    c:\/go\/src\/net\/http\/server.go:1847 +0x64d\ncreated by net\/http.(*server).serve\n    c:\/go\/src\/net\/http\/server.go:2851 +0x2fc<\/pre>\n<p><strong>\u6570\u636e\u5e93\/database.go<\/strong>\uff1a<\/p>\n<pre>package database\n\nimport (\n    \"database\/sql\"\n    \"fmt\"\n    \"github.com\/joho\/godotenv\"\n    _ \"github.com\/lib\/pq\"\n    \"log\"\n    \"restify\/utils\"\n)\n\nvar DB *sql.DB\n\n\/*\nFunction: \"Connect\".\n\nDescription:\nThe main task of the function is to initialize database connection.\n*\/\nfunc Open() () {\n    \/\/ Load environment variables from \".env\" file.\n    err := godotenv.Load(\".env\")\n    if err != nil {\n        panic(err)\n    }\n\n    \/\/ Initialize database related variables.\n    dbUser := utils.CheckEnvironmentVariable(\"PostgreSQL_USER\")\n    dbPassword := utils.CheckEnvironmentVariable(\"PostgreSQL_PASSWORD\")\n    dbHost := utils.CheckEnvironmentVariable(\"PostgreSQL_HOST\")\n    dbName := utils.CheckEnvironmentVariable(\"PostgreSQL_DATABASE_NAME\")\n\n    \/\/ Defining connection string for PostgreSQL database.\n    dbURL := fmt.Sprintf(\"user=%s password=%s host=%s dbname=%s sslmode=disable\", dbUser, dbPassword, dbHost, dbName)\n\n    \/\/ Create PostgreSQL database connection pool.\n    DB, err := sql.Open(\"postgres\", dbURL)\n    if err != nil {\n        panic(err)\n    }\n\n    \/\/ Ping PostgreSQL database to make sure it's alive.\n    err = DB.Ping()\n    if err != nil {\n        panic(err)\n    }\n\n    log.Println(\"REST API web service successfully connected to remote PostgreSQL database.\")\n}\n\n\/*\nFunction: \"Disconnect\"\n\nDescription:\nThe main task of the function is to disconnect database connection.\n*\/\nfunc Close() error {\n    return DB.Close()\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u95ee\u9898\u4f3c\u4e4e\u51fa\u5728\u6b64\u5904\u5206\u914d\u6570\u636e\u5e93\u65f6\uff1a<\/p>\n<pre>db, err := sql.open(\"postgres\", dburl)<\/pre>\n<p>\u6570\u636e\u5e93\u5728\u65b0\u8303\u56f4\u4e2d\u5b9a\u4e49\uff0c\u56e0\u6b64\u8fd9\u4f1a\u521b\u5efa\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u800c\u4e0d\u662f\u8bbe\u7f6e pkg \u7ea7\u522b\u53d8\u91cf\u3002 \u56e0\u6b64\uff0c\u4ee3\u7801 <code>open()<\/code> \u5efa\u7acb\u4e86\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002 <\/p>\n<p>\u8bd5\u8bd5\u8fd9\u4e2a\uff1a<\/p>\n<pre>var err error\nDB, err = sql.Open(\"postgres\", dbURL)<\/pre>\n<p>\u4ee5\u4e0b\u662f\u5b98\u65b9\u5f15\u7528\uff1a<\/p>\n<p>\u4e0e\u5e38\u89c4\u53d8\u91cf\u58f0\u660e\u4e0d\u540c\uff0c\u77ed\u53d8\u91cf\u58f0\u660e\u53ef\u4ee5\u91cd\u65b0\u58f0\u660e\u53d8\u91cf\uff0c\u524d\u63d0\u662f\u5b83\u4eec\u6700\u521d\u5728\u540c\u4e00\u5757\uff08\u6216\u53c2\u6570\u5217\u8868\uff0c\u5982\u679c\u8be5\u5757\u662f\u51fd\u6570\u4f53\uff09\u4e2d\u4ee5\u76f8\u540c\u7684\u7c7b\u578b\u58f0\u660e\uff0c\u5e76\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u975e-\u7a7a\u767d\u53d8\u91cf\u662f\u65b0\u7684\u3002\u56e0\u6b64\uff0c\u91cd\u65b0\u58f0\u660e\u53ea\u80fd\u51fa\u73b0\u5728\u591a\u53d8\u91cf\u77ed\u58f0\u660e\u4e2d\u3002\u91cd\u65b0\u58f0\u660e\u4e0d\u4f1a\u5f15\u5165\u65b0\u53d8\u91cf\uff1b\u5b83\u53ea\u662f\u4e3a\u539f\u59cb\u503c\u5206\u914d\u4e00\u4e2a\u65b0\u503c\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528 (2)\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528 (2) \u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528 (2) \u6765\u6e90\uff1astackoverflow 2024-04-25 23:03:38 0\u6d4f\u89c8 \u6536\u85cf \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300a\u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528 (2)\u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u662f go \u65b0\u624b\uff0c\u9700\u8981\u5e2e\u52a9\uff01 \u5728 postgresql \u6570\u636e\u5e93\u4e2d\uff0c\u6211\u6709\u4e00\u4e2a\u540d\u4e3a factors \u7684\u8868\u3002\u8be5\u8868\u6709 2 \u5217\uff08factor_id \u548c factor_name\uff09\u3002\u73b0\u5728\u6211\u5df2\u6210\u529f\u4ece go \u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u5230 postgresql \u6570\u636e\u5e93\u3002 \u5f53\u6211\u5c1d\u8bd5\u5728\u63a7\u5236\u53f0\u4e2d\u53d1\u51fa\u57fa\u4e8e\u67e5\u8be2\u7684 get \u8bf7\u6c42\u65f6\uff0c\u6211\u770b\u5230\u9519\u8bef\u3002 http:\/\/localhost:8000\/api\/factors\/?limit=5&amp;offset=1 \u6211\u54ea\u91cc\u51fa\u9519\u4e86\uff1f\u8bf7\u5e2e\u6211\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002 controllers\/factors.go\uff1a package controllers import ( &#8220;encoding\/json&#8221; &#8220;fmt&#8221; &#8220;net\/http&#8221; &#8220;restify\/models&#8221; &#8220;restify\/utils&#8221; ) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42780","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42780"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42780\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}