我如何解决无法引用未导出的字段或方法模型.golang中的Model.getDevices

entities.go

package entities

type Device struct {

Id        int    
Name      string 

}

models.go

package models

import (

"log"
"net/http"
entities "../entities"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"

)

var devices []entities.Device

type Model struct{}

func HttpInfo(r *http.Request) {

fmt.Printf("%s\t %s\t %s%s
", r.Method, r.Proto, r.Host, r.URL)

}

func (c Model) getDevices(db *gorm.DB) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) {

    setJsonHeader(w)

    HttpInfo(r)
    var devices entities.Device
    if err := db.Find(&devices).Error; err != nil {
        fmt.Println(err)
    } else {
        json.NewEncoder(w).Encode(devices)

    }
}

}

main.go

package main

import (

"log"
"net/http"

entities "./src/entities"
models "./src/models"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"

)

var devices []entities.Device

var db *gorm.DB

var err error

func main() {

// Handle Subsequent requests

fmt.Println("Api running on port 4000...")

r := mux.NewRouter().StrictSlash(true)


r.HandleFunc("/devices", model.getDevices(db)).Methods("GET")

r.HandleFunc("/devices/{id}", model.getDevice).Methods("GET")


log.Fatal(http.ListenAndServe(":4000", handlers.CORS(headers, methods, origins)(r)))

}

You simply cannot access an unexported symbol. The whole reason of being unexported is that you cannot access it. There is no way around so you must export the method. I can recommend the Tour of Go for this type of fundamental questions.