DEV Community

Samuel Fuller
Samuel Fuller

Posted on

Learning API struggling with Go html/template need advice.

I have been working on this for an embarrassingly long amount of time and I feel like I am losing my mind. I will try to explain and be brief and any help would be immensely appreciated.

I am trying to write a basic program or web app that reads from youtube data API.

Basically the objects I create in my .go file cannot always be read in the html actions after passing the object to the execute template function and I do not understand why. Nothing shows. I will paste the code below and explain in more detail.

.go objects


type Member struct {
    Name      string
    ChannelID string
    Title     string
    Thumbnail string
    Uptime    time.Time
}
type Organization struct {
    Members []Member
}

.go writers and handlers etc


func main() {
    fs := http.FileServer(http.Dir("css"))
    http.Handle("/css/", http.StripPrefix("/css/", fs))
    http.HandleFunc("/", index)
    http.HandleFunc("/view", view)
    http.ListenAndServe(":3000", nil)
}

func index(w http.ResponseWriter, r *http.Request) {
    tpl.ExecuteTemplate(w, "index.html", nil)
}

func view(w http.ResponseWriter, r *http.Request) {
    org := initMembers()

    //checking to see if org contains data I need at this point. And it does.
     fmt.Println(org)
     tpl.ExecuteTemplate(w, "view.html", org)

}

func initMembers() Organization {
    now := time.Now()
    org := Organization{
        Members: []Member{
            {
                Name:      "John",
                ChannelID: "xxxxxxxxxxxxxxxxxxxx",
                Title:     "Offline",
                Thumbnail: "empty",
                Uptime:    now,
            },
            {
                Name:      "John2",
                ChannelID: "yyyyyyyyyyyyyyyyyyyy",
                Title:     "Offline",
                Thumbnail: "empty",
                Uptime:    now,
            },
            {
                Name:      "John3",
                ChannelID: "zzzzzzzzzzzzzzzzzz",
                Title:     "Offline",
                Thumbnail: "empty",
                Uptime:    now,
            },
        },
    }
    return org
}

html file trying to range members information


{{ range .Members }}
       {{ .Name }}
{{ end }}

I omitted a lot of the code such as the call to the API and unmarshaling the json. When I unmarshal the json into a struct that is specifically for the json and pass that data to the execute template function it actually lets me make the calls on the html file. However, I do not understand why it will not work the way I have it shown above as I have tried for what feels like 1000 variations at this point and to no avail.

Navigating to the view.html page via the view func is where the issue lies. Or where the execute template call that isn't working is at least.

Overall what I am trying to do is pull specific SINGLE user data with the API call (which I've made work) and store that user data in a consolidated array object that I can iterate over in the html file (which I haven't made work). If anyone is willing to help or ELI5 I would be very grateful!! Thank you.

Top comments (2)

Collapse
 
evanj profile image
Evan Jones

I don't see anything obviously wrong on first look. template#ExecuteTemplate can error out. I would check that error. Wouldn't pass http.ResponseWriter to ExecuteTemplate as well (use a temp &bytes.Buffer{}, then copy to http.ResponseWriter on success -- io.Copy).

When I've hit snags with templating similar to this I've spit everything out, {{ . }}. What's that provide?

Isolate the issue and throw into a repo/gist and I'm happy to take a peak. You might find the issue while doing so. Good luck!

Collapse
 
ohhnate profile image
Samuel Fuller

Hey I appreciate the input and the help! I was able to fix the problem yesterday. The issue was regarding some

{{ . }}

reference on my html file that I didnt do correctly and was so certain it wasn't an issue I didn't include it on my initial post. Everything has went smoothly thus far today. I still feel a bit overwhelmed with templating and everything is starting to turn into a mess. I sorta want to work backward from where I am to get to a point where I fully understand everything. And do things more concisely. But anyways. Thank you very much for the help!