How to get Nth Element in Array?

I am brand new to the Go Language, so forgive me if this question is too simple, but I can’t figure it out.

I’m trying to customize the Gogs UI for a project I’m working on: http://try.gogs.io

There’s an object array for “Organizations”. Let’s say I have two organizations: “Company ABC” and “Company XYZ”. If I want to display both in my page I can do this:
{{range .ContextUser.Orgs}}{{.Name}}{{end}}

What I’m trying to do is just display the first one. I initially assumed that this would work:
{{.ContextUser.Orgs[0].Name}}

But it doesn’t work. Any ideas what the correct syntax is?

Robert

Hey @RobertW,

You can use the index template function, which you can find information about here: https://golang.org/pkg/text/template/#hdr-Functions.

Here’s a small example.

package main

import (
	"html/template"
	"log"
	"os"
)

func main() {
	// Create a slice of ints.
	s := []int{5, 10, 15}

	// Create a template that writes the value at index 1
	// of the data that it's given.
	t := template.Must(template.New("tmpl").Parse(`{{ index . 1 }}`))

	// Execute the template to os.Stdout and pass it `s` as it's data.
	if err := t.Execute(os.Stdout, s); err != nil {
		log.Fatalln(err)
	}
}

Benjamin, I really appreciate you responding but your answer completely confuses me.

Once again, the object array I’m working with is: .ContentUser.Orgs

What I want to do is retrieve the first element from this object array and then the Name property from that element. Using my earlier example, I want to retrieve the string “Company ABC”

I’m assuming that this could be done with code strictly inside the pairs of braces. So what code would go in here: {{ … }}

That’s what I’m trying to find out.

Thank you,

Robert

I think you are looking for something like this for example:

package main

import (
	"html/template"
	"log"
	"os"
)

type Company struct {
	Name     string
	Location string
}

func main() {
	// Create a slice of ints.
	s := []Company{
		{Name: "Company One", Location: "Australia"},
		{Name: "Company Two", Location: "America"},
	}

	t := template.Must(template.New("tmpl").Parse(`{{ with index . 0 }}{{ .Name }}{{ end }}`))

	if err := t.Execute(os.Stdout, s); err != nil {
		log.Fatalln(err)
	}
}

@RobertW, just as a side note,

In general, it’s good to actually post a snippet of the layout of your data, otherwise it can be hard to guess how you’ve actually have set things up and it makes it hard to post a more concise answer for your question.

For instance, for all I know this could be similar to your layout, but either way hopefully it helps:

package main

import (
	"html/template"
	"log"
	"os"
)

type Org struct {
	Name     string
	Location string
}

type Context struct {
	Orgs []Org
}

func main() {
	data := struct {
		Context
	}{
		Context{
			Orgs: []Org{
				{Name: "Company One", Location: "Australia"},
				{Name: "Company Two", Location: "America"},
			},
		},
	}

	t := template.Must(template.New("tmpl").Parse(`{{ with index .Context.Orgs 0 }}{{ .Name }}{{ end }}`))

	if err := t.Execute(os.Stdout, data); err != nil {
		log.Fatalln(err)
	}
}

Thank you for continuing to try to assist me.

The code I’m trying to change is directly out of Gogs. In fact, I’m actually modifying this file: https://github.com/gogits/gogs/blob/master/templates/base/head.tmpl

Once again, I’m modifying this for internal use, not to add to the open source project that is Gogs.

So here is some code that will iterate through the Organizations that belong to the User:
{{range .ContextUser.Orgs}}{{.Name}}{{end}}

Imagine that the user has 2+ of these Organizations. What I’m simply trying to do is display the Name of the first (ie. Index = 0) Organization. I find it hard to believe that a lengthy function has to be created to do this. Surely there must be a way to do this directly within the braces?

Robert

Sorry did you not see that part from my last answer? That should be what you’re looking for.

Edit: One other thing you should consider is not actually finding the value at the index inside of the template, but rather passing only the value at that index to the template, unless of course you need the whole data separately anyway.

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