Quick actions

cmd+k|ctrl+k

Navigation

Languages

Text and HTML templates / range action / Essential Go

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:16:38Z

Updated

2019-03-26T10:16:38Z

package main

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

const tmplStr = `Elements of a channel: {{ range . }}{{ . }} {{end}}
`

func main() {
	t := template.Must(template.New("range").Parse(tmplStr))

	ch := make(chan int)
	go func() {
		for i := 0; i < 3; i++ {
			ch <- i
		}
		close(ch)
	}()
	err := t.Execute(os.Stdout, ch)
	if err != nil {
		log.Fatalf("t.Execute() failed with '%s'\n", err)
	}
}
INFO