5

When I try to compile this code:

package main

import (
    "encoding/json"
    "fmt"
    "net/http"
)

func main() {
    fmt.Println("Hello, playground")
}

const (
    GET    = "GET"
    POST   = "POST"
    PUT    = "PUT"
    DELETE = "DELETE"
)

type Route struct {
    Name        string           `json:"name"`
    Method      string           `json:"method"`
    Pattern     string           `json:"pattern"`
    HandlerFunc http.HandlerFunc `json:"-"`
}

type Routes []Route

var routes = Routes{
    Route{
        Name:        "GetRoutes",
        Method:      GET,
        Pattern:     "/routes",
        HandlerFunc: GetRoutes,
    },
}

func GetRoutes(res http.ResponseWriter, req *http.Request) {
    if err := json.NewEncoder(res).Encode(routes); err != nil {
        panic(err)
    }
}

Playground

the compiler returns this error message:

main.go:36: initialization loop:
    main.go:36 routes refers to
    main.go:38 GetRoutes refers to
    main.go:36 routes

The goal of this code is to return all the routes of my API in a JSON when a client application executes a GET request on the the /routes route.

Any idea on how can I find a clean workaround to this problem?

Dave C
  • 7,729
  • 4
  • 49
  • 65
Jean Lebrument
  • 5,079
  • 8
  • 33
  • 67

2 Answers2

12

Assign the value later within init(). This will let the GetRoutes function be initialized first, then it can be assigned.

type Routes []Route

var routes Routes

func init() {
    routes = Routes{
        Route{
            Name:        "GetRoutes",
            Method:      GET,
            Pattern:     "/routes",
            HandlerFunc: GetRoutes,
        },
    }
}
JimB
  • 104,193
  • 13
  • 262
  • 255
2

Use init:

var routes Routes

func init() {
    routes = Routes{
        Route{
            Name:        "GetRoutes",
            Method:      GET,
            Pattern:     "/routes",
            HandlerFunc: GetRoutes,
        },
    }
}
Ainar-G
  • 34,563
  • 13
  • 93
  • 119