gpt4 book ai didi

go - "..."未定义("..."类型没有字段或方法 "...")

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

我正在为我的 go api 设置 crud 操作。创建所有函数后,我收到错误“app.createApplication undefined(类型 Application 没有字段或方法 createApplication)”,尽管我已经创建了它。

  • 确保变量与现有包的名称不同,正如其他有关堆栈溢出的问题所述。

API.go

package controllers

import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"

"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/lib/pq"
_ "gitlab.torq.trans.apps.ge.com/503081542/k-auth-api/models"
)

var err error

type API struct {
Database *gorm.DB
Router *mux.Router
}

func (api *API) Initialize(opts string)
{
// Initialize DB

var driver = "postgres"
var host = os.Getenv("DB_HOST")
var dbname = os.Getenv("DB_NAME")
var user = os.Getenv("DB_USER")
var password = os.Getenv("DB_PASSWORD")
var port = os.Getenv("DB_PORT")
var conn = fmt.Sprintf("host=%v dbname=%v user=%v password=%v port=%v sslmode=disable", host, dbname, user, password, port)
api.Database, err = gorm.Open(driver, conn)

if err != nil {
log.Print("failed to connect to the database")
log.Fatal(err)
}

fmt.Println("Connection estabished")

// Application Model

type Application struct {
ID string `json:"id" gorm:"primary_key"`
AccessId int64
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Name string `json:"name"`
Ci string `json:"ci"`
}

if !api.Database.HasTable(&Application{}) {
api.Database.CreateTable(&Application{})
}

// Initialize Router
api.Router = mux.NewRouter()
api.Router.HandleFunc("/api/v1/applications", api.handleApplications)
api.Router.HandleFunc("/api/v1/application/{id}", api.handleApplication)
api.Router.HandleFunc("/api/v1/applications", api.getApplications).Methods("GET")
http.Handle("/", api.Router)
}
func (api *API) getApplications(w http.ResponseWriter, r *http.Request) {
type Application struct {
ID string `json:"id" gorm:"primary_key"`
AccessId int64
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Name string `json:"name"`
Ci string `json:"ci"`
}
count, _ := strconv.Atoi(r.FormValue("count"))
start, _ := strconv.Atoi(r.FormValue("start"))

if count > 10 || count < 1 {
count = 10
}
if start < 0 {
start = 0
}

applications, err := getApplications(api.Database, start, count)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}

respondWithJSON(w, http.StatusOK, applications)
}

func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}

主.go

package main

import (
"log"
"net/http"

"github.com/gorilla/handlers"
"gitlab.torq.trans.apps.ge.com/503081542/k-auth-api/controllers"
)

var err error

func main() {
api := controllers.API{}
api.Initialize("DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_PORT sslmode=disable connect_timeout=5")
// api.Initialize("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable connect_timeout=5")

// Bind to a port and pass our router in
log.Fatal(http.ListenAndServe(":8000", handlers.CORS()(api.Router)))

if err != nil {
panic(err.Error())
}
}

最佳答案

if err := app.createApplication(api.Database); err != nil {

您的 var app 具有 Application 类型,但方法 createApplicationAPI 类型相关联。我在代码中找不到 Application 的 createApplication 方法。

关于go - "..."未定义("..."类型没有字段或方法 "..."),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53947175/

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