gpt4 book ai didi

go - 模型.go : res declared and not used

转载 作者:数据小太阳 更新时间:2023-10-29 03:46:57 27 4
gpt4 key购买 nike

虽然我更改了数据库名称和密码,但我无法重现此示例。树结构

.
├── main.go
├── main_test.go
├── models
│ └── models.go
└── utils
└── encodeutils.go

模型

func InitDB() (*sql.DB, error) {
var err error
db, err := sql.Open("postgres", "postgres://miki:pass58@localhost/mydb?sslmode=disable")
if err != nil {
return nil, err
} else {
// Create model for our URL service
stmt, err := db.Prepare("CREATE TABLE WEB_URL(ID SERIAL PRIMARY KEY, URL TEXT NOT NULL);")
if err != nil {
log.Println(err)
return nil, err
}
res, err := stmt.Exec()
if err != nil {
log.Println(err)
return nil, err
}
return db, nil
}
}

主.go

import (
"database/sql"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/PacktPublishing/Building-RESTful-Web-Services-with-Go/Chapter07/urlshortener/models"
base62 "github.com/PacktPublishing/Building-RESTful-Web-Services-with-Go/Chapter07/urlshortener/utils"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)

// DB stores the database session imformation. Needs to be initialized once
type DBClient struct {
db *sql.DB
}

// Model the record struct
type Record struct {
ID int `json:"id"`
URL string `json:"url"`
}

// GetOriginalURL fetches the original URL for the given encoded(short) string
func (driver *DBClient) GetOriginalURL(w http.ResponseWriter, r *http.Request) {
var url string
vars := mux.Vars(r)
// Get ID from base62 string
id := base62.ToBase10(vars["encoded_string"])
err := driver.db.QueryRow("SELECT url FROM web_url WHERE id = $1", id).Scan(&url)
// Handle response details
if err != nil {
w.Write([]byte(err.Error()))
} else {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
responseMap := map[string]interface{}{"url": url}
response, _ := json.Marshal(responseMap)
w.Write(response)
}
}

// GenerateShortURL adds URL to DB and gives back shortened string
func (driver *DBClient) GenerateShortURL(w http.ResponseWriter, r *http.Request) {
var id int
var record Record
postBody, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(postBody, &record)
err := driver.db.QueryRow("INSERT INTO web_url(url) VALUES($1) RETURNING id", record.URL).Scan(&id)
responseMap := map[string]interface{}{"encoded_string": base62.ToBase62(id)}
if err != nil {
w.Write([]byte(err.Error()))
} else {
w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(responseMap)
w.Write(response)
}
}

func main() {
db, err := models.InitDB()
if err != nil {
panic(err)
}
dbclient := &DBClient{db: db}
if err != nil {
panic(err)
}
defer db.Close()
// Create a new router
r := mux.NewRouter()
// Attach an elegant path with handler
r.HandleFunc("/v1/short/{encoded_string:[a-zA-Z0-9]*}", dbclient.GetOriginalURL).Methods("GET")
r.HandleFunc("/v1/short", dbclient.GenerateShortURL).Methods("POST")
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}

当我运行时

go run /home/miki/go/src/github.com/PacktPublishing/Building-RESTful-Web-Services-with-Go/Chapter07/urlshortener/main.go

我得到了

go/src/github.com/PacktPublishing/Building-RESTful-Web-Services-with-Go/Chapter07/urlshortener/models/models.go:22:3: res declared and not used

为什么?模型在 main 中初始化。

只是为了展示postqresqlmydb=#\c mydb\dt

You are now connected to database "mydb" as user "postgres".
List of relations
Schema | Name | Type | Owner
--------+---------+-------+-------
public | web_url | table | miki

如果我尝试

_, err = stmt.Exec()
if err != nil {
log.Println(err)
return nil, err
}
return db, nil

然后

2019/04/09 09:35:58 pq: relation "web_url" already exists
panic: pq: relation "web_url" already exists

最佳答案

报错信息清楚,res,model中定义,见下文,已声明未使用

res, err := stmt.Exec()
if err != nil {
log.Println(err)
return nil, err
}
return db, nil

如果你不关心res,可以用_忽略它

_, err = stmt.Exec()
if err != nil {
log.Println(err)
return nil, err
}
return db, nil

关于go - 模型.go : res declared and not used,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587207/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com