Is there a golang equivalent for AngularJS’ ui-sref[SOLVED]

Hey there,

Is there a golang way/equivalent for AngularJS’ ui-sref

I have created a list in a html template like this:

               {{range .}}
                    <a class="postal_green" href="/transporterinput">
                        <h5>Name: {{.CompanyName}}</h5>
                        <h5>Address: {{.CompanyAddress}} </h5>
                        <h4>ID: {{.ID}}</h4>

This renders as a list, each item being a html <a> tag. Clicking one of the list items takes me via
http.HandleFunc("/transporterinput", controllers.TransporterInputController)
to the desired page; however, I want to pass the ID with it as well, like you can do with AngularJS, e.g.
<a ui-sref="app.transporterInput({id:})”>


You can do one of two things.

Option A: Put the ID in the URL and then parse it with your handle. This would end up with something like:

<a class="postal_green" href="/transporterinput/{{.ID}}">

And then updating your handle func to be something like:

http.HandleFunc("/transporterinput/", controllers.TransporterInputController)

Then you would need to parse that portion of the URL in your handler function.

Option B: Use a URL parameter.

<a class="postal_green" href="/transporterinput?id={{.ID}}">

Then your existing handler would work, but you will need to use something like ParseForm to parse the URL parameters.

Does that help get you on the right track?

1 Like

Yes, definitely man, thanks a lot!

I’m using Option A and with fmt.Println(req.URL) I see /transporterinput/2 where 2 is the ID. I’m now delving through the net/url documentation to see how to get the 2 out of the whole lot by going through the URL struct. If you have any suggestions please let me know; perhaps I should write a PERL type function to split the whole lot?

After fiddling around with Option A, I found Option B the better one. So indeed
<a class="postal_green" href="/transporterinput/?id={{.ID}}"> (Note the second / !) and
http.HandleFunc("/transporterinput/", controllers.TransporterInputController)

In the handler function

// just checking 
err := req.ParseForm()
if err != nil { panic(err) }

theID := req.FormValue(“id”)

Which yields the wanted result.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.