I feel a little silly as this should be an easy one, however I just started with go and can't figure it out.
package main
import "fmt"
type Question struct {
q []string
a []string
}
func (item *Question) Add(q string, a string) {
n := len(item.q)
item.q[n] := q
item.a[n] := a
}
func main() {
var q Question
q.Add("A?", "B.")
}
When Compiling it gives the errors:
q.go:11:12: error: expected ';' or '}' or newline q.go:12:12: error: expected ';' or '}' or newline
that refers to the opening brace of item.q[n] := q and the following line.
I'm certain that I'm using slices incorrectly as it works fine with a simple string instead, but I'm not sure how to fix it.
edit: I have re-implemented it using StringVectors as per Pat Notz's advice and it works well. The following is the working code:
package main
import (
fmt "fmt"
vector "container/vector"
)
type Question struct {
q vector.StringVector
a vector.StringVector
}
func (item *Question) Add(q string, a string) {
item.q.Push(q)
item.a.Push(a)
}
func (item *Question) Print(index int) {
if index >= item.q.Len() {
return
}
fmt.Printf("Question: %s\nAnswer: %s\n", item.q.At(index), item.a.At(index))
}
func main() {
var q Question
q.Add("A?", "B.")
q.Print(0)
}