0

I am trying to use Gorp to get all gym classes. Gym classes have a class type so I run a second query to retrieve them. I get all the class types back, but the final assingment is not working for some reason.

package entities

import (
    "fmt"
    "github.com/coopernurse/gorp"
    "time"
)

type Class struct {
    Id                int
    ClassTypeId       int
    ClassType         ClassType
    VideoPath         string
    VideoSize         int
    Duration          float64
    CreatedAt         time.Time
    VisibleAt         time.Time
    NoLongerVisibleAt time.Time
}

func LatestClasses(dbmap *gorp.DbMap) *[]Class {
    var classes []Class
    query := "SELECT * FROM Class"

    _, err := dbmap.Select(&classes, query)
    if err != nil {
        panic(err)
    }

    for _, class := range classes {
        classTypeForClass(dbmap, &class)
    }

    return &classes
}

func classTypeForClass(dbmap *gorp.DbMap, class *Class) {
    var classType ClassType
    query := "SELECT * FROM ClassType "
    query += "WHERE Id=?"

    err := dbmap.SelectOne(&classType, query, class.ClassTypeId)
    if err != nil {
        panic(err)
    }
    fmt.Println(classType) <<<<<<<<<<< Lists Yoga, Pilates etc.
    class.ClassType = classType <<<<<<<< Seemingly does nothing

}

UPDATE

The ClassType struct looks like this:

package entities

import (
    "time"
)

type ClassType struct {
    Id           int
    Code         string
    Name         string
    InstructorId int
    CreatedAt    time.Time
}

ANOTHER UPDATE

I display the data as follows:

<h1>
    Latest Classes
</h1>
<hr/>


{{ range .}}
    {{.VideoPath}}
    <br>
    {{.ClassType.Name}}
    <br>
    {{.VideoSize}}
    <br>
    {{.Duration}}
    <br>
    {{.CreatedAt}}
    {{.NoLongerVisibleAt}}
<br><br>
{{end}}
5
  • Show us the classType definition. Commented Jan 21, 2014 at 9:57
  • I have added the ClassType definition. Commented Jan 21, 2014 at 10:08
  • Why do you say that the assignment does nothing? How you verify that? Commented Jan 21, 2014 at 10:23
  • I added the html snippet Commented Jan 21, 2014 at 10:32
  • Where are you passing the Class struct to the template? Commented Jan 21, 2014 at 11:50

1 Answer 1

2

The assignment works properly, but you are not assigning it to the proper object: In your loop

for _, class := range classes { classTypeForClass(dbmap, &class) }

class is a copy of the elements in the classes slice. Your classTypeForClass(dbmap, &class) properly assigns the class to this copy and the copy is discarded at the end of the loop body.

Try something like

for i := range classes {
    classTypeForClass(dbmap, &(classes[i]))
}

or maybe nicer: Have classTypeForClass return the class and just assign it like

for i := range classes {
    classes[i].ClassType = classTypeForClass(dbmap, &(classes[i]))
}

(Maybe even passing class as a value, not as a pointer.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.