duanbai1974 2018-12-27 15:13
浏览 77

“…”未定义(“……”类型没有字段或方法“…”)

I am setting up crud operations for my go api. After creating all the functions, I get error "app.createApplication undefined (type Application has no field or method createApplication)", although I have created it.

  • Made sure that variables do not have the same name as existing packages as other questions on stack overflow has stated.

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)
}

main.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())
    }
}
  • 写回答

2条回答 默认 最新

  • dtziv24262 2018-12-27 15:32
    关注
    if err := app.createApplication(api.Database); err != nil {
    

    Your var app has Application type but method createApplication linked with API type. I cannot find createApplication method for Application in the code.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?