Skip to content

how to print string which in slice (html/template) #34317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
angelandy opened this issue Sep 16, 2019 · 3 comments
Closed

how to print string which in slice (html/template) #34317

angelandy opened this issue Sep 16, 2019 · 3 comments

Comments

@angelandy
Copy link

What version of Go are you using (go version)?

$ go version
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/angelandy/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/media/angelandy/4f95d6b7-a5ba-4a10-9232-6017dcced1d8/angelandy/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go-1.12"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.12/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build271133787=/tmp/go-build -gno-record-gcc-switches"

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

package main

import (
"os"
"text/template"
)

func main() {
// Define a template.
const tmpl = this is a test {{.list[1]}}

// Prepare some data to insert into the template.

var UserList = []string{
	"aaaaa",
	"bbbbb",
	"ccccc",
	"ddddd",
}
td := map[string]interface{}{
	"name":    "test",
	"list":    UserList,
}


// Create a new template and parse the letter into it.
t := template.Must(template.New("tmpl").Parse(tmpl))

t.Execute(os.Stdout, td)

}

What did you expect to see?

i want : " this is a test bbbbb"

What did you see instead?

@angelandy
Copy link
Author

but It failed
panic:
unexpected bad character U+005B '[' in command

@ianlancetaylor
Copy link
Contributor

We don't use the issue tracker for questions. Please see https://golang.org/wiki/Questions.

In this case the problem is that the template language doesn't support indexing like [1].

I think what you want is https://play.golang.org/p/Xxn_ksCzWJL.

@angelandy
Copy link
Author

well, thank you very much

@golang golang locked and limited conversation to collaborators Sep 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants