Range Over Two Slices In template in same html table

type User struct {
	Id               uint64 `json:"id" gorm:"primaryKey"`
	FullName  string `json:"full_name"`
	Moniker     string `json:"moniker"`
	Contact     string `json:"contact"`
	Email        string `json:"email"`

type UserAccount struct {
	Id                       uint64 `gorm:"primaryKey"`
	CompanyRank  string `json:"company_rank"`
	SystemLevel     string `json:"system_level"`
	Department       string `json:"department"`
	UserRefer         int    `json:"fkuser_id"`
	User                  User   `gorm:"foreignKey:UserRefer"`

This the method that gets list of all records in UserAccount table in database

func (h handler) GetUserAccounts(ctx *gin.Context) {

userAccounts := []model.UserAccount{}
var users []model.User
links := model.BodyNLinks()
links.Title = "Roles List"
links.Heading = "Roles List"

if result := h.DB.Find(&userAccounts); result.Error != nil {
	ctx.AbortWithError(http.StatusNotFound, result.Error)

for _, v := range userAccounts {
	user := model.User{}
	if result := h.DB.Find(&user, v.UserRefer); result.Error != nil {
		ctx.AbortWithError(http.StatusNotFound, result.Error)
	users = append(users, user)

ctx.HTML(200, "useraccount/list.html", gin.H{
	"userAccounts": userAccounts,
	"users":        users,
	"link":         links,

The html page used to display records from user_accounts database

{{ define "useraccount/list.html" }}
{{ template "01Temps/header.html" . }}
    {{range $xi := .users}}
    <li>{{$xi.FullName}} {{$xi.Contact}}</li>
</ul> </br>
<table na border="1">
    <tr align="right">
    <tbody id="myTbl">
        {{range $xi := .userAccounts}}
        <tr align="left">
                <a href="/user/getrecord/{{$xi.UserRefer}}"
                    onclick=" return confirm('Redirect to USER:{{$xi.UserRefer}} ?')">

            {{range $xu := .users}}

            <td><a href="/useraccount/updaterecord/{{$xi.Id}}"
                    onclick="return confirm('Update USER:{{$xi.UserRefer}} ?')">Update</a>
                <a href="/useraccount/deleterecord/{{$xi.Id}}"
                    onclick="return confirm('Delete USER:{{$xi.UserRefer}} ?')">Del</a>
                <a href="/useraccount/getrecord/{{$xi.Id}}"
                    onclick="return confirm('Display USER:{{$xi.UserRefer}} ?')">Display</a>
{{ template "01Temps/footer.html" . }}

Why can’t I display users and userAccounts data from controller into same template table rows.
In UsertAccount struct, if I was to

UserRefer int json:"fkuser_id"
UserFullNameRefer string json:"fkuser_full_name"
User User gorm:"foreignKey:UserRefer"

How would I add UserFullNameRefer as a foreign column from User struct

1 Like