Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
waffle enthusiast
Nov 16, 2007



Sub Par posted:

I am new to Go and have a dumb question. I have a template in which I would like to render something resembling a calendar. As part of it, I'd like to generate 31 identical divs. I don't really understand how to use the range operator to do this - the internet seems to be replete with examples of how to use range to iterate over slices and whatnot, but nothing about how to do what amounts to a basic for loop. Help?

go template docs posted:

{{range pipeline}} T1 {{end}}
The value of the pipeline must be an array, slice, map, or channel.
If the value of the pipeline has length zero, nothing is output;
otherwise, dot is set to the successive elements of the array,
slice, or map and T1 is executed. If the value is a map and the
keys are of basic type with a defined order ("comparable"), the
elements will be visited in sorted key order.

This is idiomatic to Go:

code:
package main

import (
	"os"
	"text/template"
)

type Calendar struct {
	Days []int
}

func main() {
	cal := Calendar{Days: []int{1, 2, 3, 4, 5}}
	tmpl, err := template.New("days").Parse("{{ range $index, $elem := .Days }}Day {{ $elem }}\n{{end}}")
	if err != nil {
		panic(err)
	}
	if err := tmpl.Execute(os.Stdout, cal); err != nil {
		panic(err)
	}
}
You could also use an anonymous struct if you're never going to reuse it.

Adbot
ADBOT LOVES YOU

waffle enthusiast
Nov 16, 2007



Docker, rkt, Kubernetes, InfluxDB, Prometheus, etcd, pretty much everything by Hashicorp now. It's really gaining traction where it counts — in large scale projects built to be used by a poo poo load of people.

Personally, I think it's fun to read and write, and it gets to the point. I echo Bozart in that it's my go-to language now. It works pretty well for API's fronting SPA's and for small tasks.

waffle enthusiast
Nov 16, 2007



If you're unsure about the number of things you're going to pass on the channel, you can also use a WaitGroup. Notice that the WaitGroup needs to live in its own goroutine in order to be able to close the channel:

code:
package main

import (
	"fmt"
	"sync"
)

type hostInfo struct {
	hostname string
	platform string
}

func pinger(data hostInfo, done chan hostInfo, wg *sync.WaitGroup) {
	defer wg.Done()
	// Code to do some ping stuff...
	done <- data
}

func main() {
	pchan := make(chan hostInfo)
	var wg sync.WaitGroup

	someData := []hostInfo{}
	someData = append(someData, hostInfo{"10.1.1.1", "linux"})
	someData = append(someData, hostInfo{"10.1.1.2", "linux"})
	someData = append(someData, hostInfo{"10.1.1.3", "linux"})
	someData = append(someData, hostInfo{"10.1.1.4", "linux"})

	for _, v := range someData {
		wg.Add(1)
		go pinger(v, pchan, &wg)
	}

	go func() {
		wg.Wait()
		close(pchan)
	}()

	for data := range pchan {
		fmt.Printf("Finished with %s of type %s.\n", data.hostname, data.platform)
	}

}

waffle enthusiast
Nov 16, 2007



I think glide is the up and comer. At Gophercon more people raised their hands when Kelsey asked about glide vs godep, but I think that's inertia as opposed to momentum. We use glide at work.

First Time Caller posted:

Really cool poo poo. Why is thread so inactive, go is awesome.

It's still a pretty niche language by overall usage, even though it seems like literally everything new and awesome is being written in it: Docker, Kubernetes, Prometheus, pretty much everything in the Hashicorp stack now.

waffle enthusiast
Nov 16, 2007



MALE SHOEGAZE posted:

My main complain about go these days is that the community is full of luddites who are hostile towards both useful language features and anyone introducing a new library that attempts to do...anything. My main experiences are on the golang reddit, though, which is no longer an official community. So maybe it's not representative of the real go community.

I don't think the /r/golang was ever a real community to begin with. Not very many people, and most of the content was just hotlinks to random projects. The fact that they "killed" it because of the Reddit CEO's thing was just silly.

The Golang Slack is a pretty good resource.

waffle enthusiast
Nov 16, 2007



https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package might be worth a read.

waffle enthusiast
Nov 16, 2007



^^ a good post.

The package management piece is in the process of being baked now. I won’t go into the craziness, but there was “dep” for a while, which is now in the process of being replaced with vgo.

waffle enthusiast
Nov 16, 2007



e: very wrong thread.

Adbot
ADBOT LOVES YOU

waffle enthusiast
Nov 16, 2007



Don’t remember if this was posted before but it’s making the rounds now. Dave Cheney posted an article constructed from his practical go talk. It’s really, really good. Goes into the philosophy of Go, as well as idiomatic concepts. Well worth your time.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply