虽然更改了数据库名称和密码,但无法重现此示例。树结构
"jest": {
"setupFiles": [
"<rootDir>/test/setupTests.js"
],
}
模型
.
├── main.go
├── main_test.go
├── models
│ └── models.go
└── utils
└── encodeutils.go
main.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
}
}
我跑步时
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
为什么?模型在main中初始化。
仅显示postqresql mydb =#\ c mydb \ dt
go/src/github.com/PacktPublishing/Building-RESTful-Web-Services-with-Go/Chapter07/urlshortener/models/models.go:22:3: res declared and not used
如果我尝试
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
答案 0 :(得分:2)
错误消息是明确的,res,在模型中定义,请参见下文,已声明且未使用
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