1
votes

The issue here appears to be syntax, this is day 3 of coding in Go for me. The for loop give me the red underline under for and the message says: "expected declaration found, 'for'"

Can someone explain to me why and/or where I went wrong?

func getAllWithTag(w http.ResponseWriter, r *http.Requests) {
    fmt.Println("Results with tag: *Tags)
}

var params = mux.Vars(r)

for _, tag := range tags {
  if *Tags == nil {
    Jason.NewEncoder(w).Encode("error: The tag parameter is required")
  }     
}

In short I am trying to make the tags a required field when searching but that part isn't important til I resolve this error.

1
The error most likely has to do with whatever code is just preceding the for. Please include that. - Hymns For Disco
Just updated it - grandmasternik
fmt.Println("Results with tag: *Tags) seems a double quote was missing - user338371
Semicolons aren't used in Go. - grandmasternik

1 Answers

0
votes

You cannot put for loops in the global scope (outside of any function). If you want code that runs automatically in that package before the program starts, put it inside of an init function.

func init() {
    for {...}
}