Bryan
Bryan

Reputation: 93

How to concatenate a href in a go html template

I'd really appreciate if somebody could help me out with this. I've been going through similar questions asked but none seem to help with my problem.

I have a User table in a go template. In the right most column I have two buttons to update and delete a user. I want these buttons to point to a url that will update and delete that specific user. So I need to have the user id in the url.

This is the code:

<table class="table table-striped">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Date Of Birth</th>
        <th>County</th>
        <th>Books</th>
        <th>Perform Action</th>
      </tr>
    </thead>
    <tbody>
      {{ range .Users}}
      <tr>
          <td>{{ .Id }}</td>
          <td>{{ .Name }}</td>
          <td>{{ .DateOfBirth }}</td>
          <td>{{ .County }}</td>
          <td>
            {{if .Books}}
              {{ range $id, $book := .Books }}
                <li>{{ $book.Title }}</li>
              {{ end }}
            {{else}}
            No Books
            {{end}}
          </td>
          {{ $updateUrl := "http://localhost:8080/library/updateuser/" + .Id }}
          <td><a href="http://localhost:8080/library/deleteuser/"+{{ .Id }} class="btn btn-outline-dark ml-3">Update User</a><a href="http://localhost:8080/library/deleteuser/"+{{ .Id }} class="btn btn-outline-dark ml-3">Delete User</a></td>
      </tr>
      {{ end}}
    </tbody>
  </table>

In the 5th line up from the bottom you'll see my incorrect attempt at concatenating the href with the id.

Upvotes: 0

Views: 3137

Answers (1)

Burak Serdar
Burak Serdar

Reputation: 51592

You cannot concatenate strings using +. This should work:

<a href="http://localhost:8080/library/deleteuser/{{ .Id }}">

Upvotes: 3

Related Questions