-4

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\n", 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)))

}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Kumar Pun
  • 11
  • 2

1 Answers1

3

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 question.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Volker
  • 40,468
  • 7
  • 81
  • 87