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
Mao Zedong Thot
Oct 16, 2008


Voted Worst Mom posted:

It doesn't matter what gofmt does because it does it consistently and that's the point. There are some bugs but that's not one of them.

Yeah, caring about how gofmt is formatting your poo poo indicates you don't understand how great gofmt is.

I have been writing a bit of golang recently, and I find it decently enjoyable. For the most part I can appreciate the language for what it is and isn't, it fills a like-C-but-not niche pretty well. I will bandwagon missing generics though, having to use (literally) the exact same code in 3 spots because I'm doing the same thing to 3 maps with a different value type is shameful (though map[string]interface{} is an even worse solution).

Adbot
ADBOT LOVES YOU

Linear Zoetrope
Nov 28, 2011

A hero must cook
You think that's bad, try working on a project that needs things like matrices. The `go generate` command has made things a bit easier, but it's extremely silly to have to write your own template generation code to implement the exact same algorithms for float/big/complex/whatever.

(The code can deviate at points; especially for things like constants, but the points where the code diverges are much more sparse than cases where it's identical).

Deus Rex
Mar 5, 2005

polpotpotpotpotpot posted:

Yeah, caring about how gofmt is formatting your poo poo indicates you don't understand how great gofmt is.

That's not a very strong argument. If gofmt formatted each of your files into a single line, would you still feel that way?

It's possible to simultaneously feel that a tool like gofmt is a great idea, and also that the formatting decisions it makes are problematic at times.

Mao Zedong Thot
Oct 16, 2008


Deus Rex posted:

That's not a very strong argument. If gofmt formatted each of your files into a single line, would you still feel that way?

It's possible to simultaneously feel that a tool like gofmt is a great idea, and also that the formatting decisions it makes are problematic at times.

Sure, my point is simply that it Just Works and is Pretty Good and why the hell does anybody feel like bikeshedding the smooshing of x*x.

Tangential hilarity, re: someone asking for map/filter:

https://groups.google.com/forum/#!msg/golang-nuts/RKymTuSCHS0/MylwD97xYqUJ posted:

The modern programmer thinks a newline is a thousand times harder to
type than any other character. If instead you take a newline as only
one keystroke, which it is, the fact that your program might not fit
on one line is a bearable burden.

-rob

https://groups.google.com/d/msg/golang-nuts/RKymTuSCHS0/BXbkvn3ckOkJ posted:

I would say the Go designers are frightened of newlines. In particular the one in front of the hanging brace.

sarehu
Apr 20, 2007

(call/cc call/cc)
This is the best reason I have so far for why Go should not have generics: http://forums.somethingawful.com/showthread.php?threadid=3693703&pagenumber=2#post444927286

Deus Rex
Mar 5, 2005

sarehu posted:

This is the best reason I have so far for why Go should not have generics: http://forums.somethingawful.com/showthread.php?threadid=3693703&pagenumber=2#post444927286

What does that have to do with generics?

sarehu
Apr 20, 2007

(call/cc call/cc)

Deus Rex posted:

What does that have to do with generics?

You can't write your functional programming libraries without them.

Pollyanna
Mar 5, 2005

Milk's on them.


Clojure implements efficient persistent data structures as part of the language so clearly they should have used Clojure. :smug:

Winkle-Daddy
Mar 10, 2007
I just started learning Go this last week and like it quite a bit. I'm hoping it can fill the wasteland void that we currently use Ruby for in a lot of places. There's a whole lot of conversations I have that go something like "Oh yeah, I wrote a script for that, just git clone the repo it's in and use it......well yeah, you have to do bundle install first to get the required gems...no bundler is a thing you have to install....got it? Great. Hmmm, it's still not working? What version of Ruby are you running? Ah, just point RVM at a new version....oh, you installed a system Ruby? gently caress. Well, try this Python thing instead, yeah, that won't work on RHEL 5.6 because Python is too old."

I think any hit my productivity takes to learn my way around go will be made up for in that I won't be troubleshooting installs of Ruby/Python on other developer systems all goddamn day. Whelp, that's my story! I just wanted to say that as someone who is coming from a Python/C background, Go feels super duper natural and I like it a lot!

Winkle-Daddy
Mar 10, 2007
I'm reading some about how interfaces work here: http://www.golangbootcamp.com/book/interfaces and had a question about this.

To save everyone from clicking on the link, it provides some example code like this:
code:
package main

import (
	"fmt"
)

type User struct {
	FirstName, LastName string
}

func (u *User) Name() string {
	return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
}

type Namer interface {
	Name() string
}

func Greet(n Namer) string {
	return fmt.Sprintf("Dear %s", n.Name())
}

func main() {
	u := &User{"Matt", "Aimonetti"}
	fmt.Println(Greet(u))
}
My question is, can the Name() function take parameters through an interface? What would be the suggested method for passing arguments be? The only way I could think to do it in this example would be to extend the User struct to include any options I want set, then set them when I dereference into the variable "u" which (should) make it accessible to the function the interface calls. That feels kind of dirty though, would that be considered the "go way" of doing things?

e: sorry if this is a dumb question, it's just that everyone says "Start doing things the go way to learn better!" and I'm not exactly sure what the go way is in this case.

Winkle-Daddy fucked around with this message at 22:18 on Aug 28, 2015

Zaxxon
Feb 14, 2004

Wir Tanzen Mekanik

Winkle-Daddy posted:

My question is, can the Name() function take parameters through an interface? What would be the suggested method for passing arguments be? The only way I could think to do it in this example would be to extend the User struct to include any options I want set, then set them when I dereference into the variable "u" which (should) make it accessible to the function the interface calls. That feels kind of dirty though, would that be considered the "go way" of doing things?

e: sorry if this is a dumb question, it's just that everyone says "Start doing things the go way to learn better!" and I'm not exactly sure what the go way is in this case.

I'm not sure what you mean here but you can just pass arguments like a normal function.

code:
package main

import (
	"fmt"
)

type User struct {
	FirstName, LastName string
}

func (u *User) Name(pointlessArg int) string {
	return fmt.Sprintf("%s %s %d", u.FirstName, u.LastName, pointlessArg)
}

type Namer interface {
	Name(int) string
}

func Greet(n Namer) string {
	return fmt.Sprintf("Dear %s", n.Name(15))
}

func main() {
	u := &User{"Matt", "Aimonetti"}
	fmt.Println(Greet(u))
}

Winkle-Daddy
Mar 10, 2007
Hmm, okay, but here:

code:
func main() {
	u := &User{"Matt", "Aimonetti"}
	fmt.Println(Greet(u))
}
What's the syntax for getting my argument into the function since u was passed into Greet? This part is my specific question, which I probably could have asked better.

Zaxxon
Feb 14, 2004

Wir Tanzen Mekanik

Winkle-Daddy posted:

Hmm, okay, but here:

code:
func main() {
	u := &User{"Matt", "Aimonetti"}
	fmt.Println(Greet(u))
}
What's the syntax for getting my argument into the function since u was passed into Greet? This part is my specific question, which I probably could have asked better.

well, "Greet" doesn't really support this, but greet isn't part of the interface.

All the interface declaration states is that if "x" is a Namer you will be able to do x.Name() and get a string. If the interface states that x.Name takes some arguments it's up to the caller to pass those arguments.

Winkle-Daddy
Mar 10, 2007
Oh duh. Okay. Thank you! :)

NickPlowswell
Sep 14, 2014

The exoplanet has failed :qq:
I tend to use interfaces a ton when doing stuff like event passing/update methods.

For example, I have some "widget" like structs I use with termbox-go

The interface for them is

code:
type Component interface {
	Draw()
	Event(termbox.Event) bool
}
Then all of my components can live in a struct I call Screen. This allows me to "send" events to every active component and call update functions regardless of which component it is. Additionally, you can do cool things using embedded structs.

With the component example:

code:
type GenericComponent struct{}

func (g *GenericComponent) Draw() {}
func (g *GenericComponent) Event(ev termbox.Event) bool { return true }

type SpecificComponent {
	GenericComponent
}
Now, even though we don't make methods for our SpecificComponent it still satisfies the interface. This is useful if you know you don't need to catch events or draw.

NickPlowswell fucked around with this message at 10:50 on Sep 8, 2015

First Time Caller
Nov 1, 2004

http://www.gopl.io/

This book is releasing in 4 days. Anyone excited?

duck monster
Dec 15, 2004

First Time Caller posted:

http://www.gopl.io/

This book is releasing in 4 days. Anyone excited?

Brian Kernigan co wrote it? Well thats got my attention!

Sub Par
Jul 18, 2001


Dinosaur Gum
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?

Karate Bastard
Jul 31, 2007

Soiled Meat
https://tour.golang.org/flowcontrol/1

The entire go tour is pretty rad. Highly recommended.

e: vvvvv oh, sorry, right. I misread your post and got the wrong idea. I haven't used templates.

Karate Bastard fucked around with this message at 19:51 on Jan 18, 2016

Sub Par
Jul 18, 2001


Dinosaur Gum
Edit: nevermind, I'm just going to do this in JS.

Sub Par fucked around with this message at 21:11 on Jan 18, 2016

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Sub Par posted:

Edit: nevermind, I'm just going to do this in JS.

Go everyone!

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.

Count Thrashula
Jun 1, 2003

Death is nothing compared to vindication.
Buglord
Go is confusing to me. I just got a coupon code for a Udemy course about it, and it seems super great from the outset, and a lot of companies are using it. But... only 5 pages here, and most of it is complaining about the language. Did it just never get any traction? Is it dying out?

Linear Zoetrope
Nov 28, 2011

A hero must cook

COOL CORN posted:

Go is confusing to me. I just got a coupon code for a Udemy course about it, and it seems super great from the outset, and a lot of companies are using it. But... only 5 pages here, and most of it is complaining about the language. Did it just never get any traction? Is it dying out?

It is very good at certain things. Go is conceptually simple and lightweight. Unfortunately, its devotion to "simplicity" is somewhat slavish which causes some huge problems for certain types of code. It can also cause performance issues because, despite being a compiled language, almost everything generic has to use vtables at best and aggressive type conversion and/or reflection at worst unless you want to roll your own template generation code.

Its interoperability with C is also... idiosyncratic.

sunaurus
Feb 13, 2012

Oh great, another bookah.

COOL CORN posted:

Go is confusing to me. I just got a coupon code for a Udemy course about it, and it seems super great from the outset, and a lot of companies are using it. But... only 5 pages here, and most of it is complaining about the language. Did it just never get any traction? Is it dying out?

I don't think it's dying out, in fact, I get the impression that it's quite popular with new start-ups. My opinion is that you can't write "beautiful" code in go (which is of course very subjective), but that doesn't really mean that it's a bad language.

Mao Zedong Thot
Oct 16, 2008


Go is really good for infrastructure stuff. We write and use a ton of tooling in Go both stuff we've written (logging, config mgmt, virtualenv fuckery, load balancing) and OSS like etcd, terraform, etc.

Anybody that's spent much time with our Go code really starts to loathe the hundreds of thousands of lines of python we have. As our team has grown we get bit more and more by bullshit that comes out of bigger teams mixed with weak types, no compilation, etc. Not to mention we end up CPU bound in a ton of places.

Go rulez basically.

Bozart
Oct 28, 2006

Give me the finger.

COOL CORN posted:

Go is confusing to me. I just got a coupon code for a Udemy course about it, and it seems super great from the outset, and a lot of companies are using it. But... only 5 pages here, and most of it is complaining about the language. Did it just never get any traction? Is it dying out?

Just not much activity in this thread to be honest. The reddit group has been growing in quality (particularly since the devs started doing AMAs) and it is on YC a lot.

Go is really good at some stuff - especially deployment!!! - pretty good at most stuff, and meh at some stuff. If you're looking for language feature xxx in it, you'll be one of the people hating on it. I absolutely love how readable random github repos in go are. I love the tooling. I'm at the point where it is my go to language, unless the task is an extremely poor fit.

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.

bonds0097
Oct 23, 2010

I would cry but I don't think I can spare the moisture.
Pillbug
Just got an offer for a Go dev job. It's been my language of choice for the past two years on all my personal projects so I'm really excited at the prospect of using it in a more advanced production context.

Linear Zoetrope
Nov 28, 2011

A hero must cook
Go is basically just really good until it's not. It's amazing to me how easy it is to keep it in your head and read someone else's code, but once you hit a HERE BE DRAGONS area, have fun. One HERE BE DRAGONS area is interoperability with any C library that expects all calls to be on the main thread (like anything involving windowing systems on OS X). You can do it by locking the OS thread in the init function, but you can't really do that as a modular library, it pretty much worms its way into the structure of any program with it as a subdependency.

Winkle-Daddy
Mar 10, 2007
I've learned a lot more about Go and like it quite a bit. But I ran into a weird thing that I'm having a hard time googling the 'correct' way to do it. Hopefully someone can help out. I have a struct that I want to use for reading/writing config files. My struct is like this:
code:
type Configuration struct {
	ValueOne   string
	ValueArray    []string
	ValueThree    string
	IntValue int
}
This should allow me to unmarshall the following json into this struct:
code:
{
    "ValueOne": "foo"
    "ValueArray": ["foo", "bar"]
    "ValueThree": "foobar"
    "IntValue": 5
}
But what I'm having a hard time figuring out is how I marshall the data if I want to just write an empty config file on first run. For example:
code:
dataToWrite := Configuration{"foo",
			["foo", "bar"], // ideally this would just be [], or some other empty value intended to be replaced
			"foobar",
			5}
d, _ := json.MarshalIndent(dataToWrite, "", "    ")
err := ioutil.WriteFile(configFile, d, 0600)
I did find this excellent blog post. With some re-working I could go this route, but it seems silly that if I have a defined struct I don't use it to marshall and unmarshall my data as opposed to something like this (which should work):
code:
dataToWrite := []byte(`{"ValueOne":"foo","ValueArray":["foo","bar"],"ValueThree":"foobar","IntValue":5}`)
Do I just settle for what I think is the less clean looking option or is there a way to fix what I am doing?

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
You're just using the wrong syntax for the composite literal, if I'm understanding the question:

https://play.golang.org/p/uebkyYxuCN

Count Thrashula
Jun 1, 2003

Death is nothing compared to vindication.
Buglord
Does anyone use Go for game development? I'm curious what the most popular use cases of Go are at the moment - I guess mostly web-based?

Winkle-Daddy
Mar 10, 2007

Lonely Wolf posted:

You're just using the wrong syntax for the composite literal, if I'm understanding the question:

https://play.golang.org/p/uebkyYxuCN

Oh! That makes perfect sense looking at it now. Heh. Thanks! This is what happens when you're coming from Python, I guess...

COOL CORN posted:

Does anyone use Go for game development? I'm curious what the most popular use cases of Go are at the moment - I guess mostly web-based?

We have found that go is best used as part of your service oriented architecture stack. It's really easy to get individual go binaries deployed and controlled through systemd that do everything from host simple web services to doing more advanced manipulation. It's not very good for game dev, and UI libs aren't well supported like at all due to how big your binary becomes due to the static linking involved. Someone who has actually tried could probably elaborate more, but I see it as 90% replacing your bash magic glue in your tech stack.

Winkle-Daddy fucked around with this message at 17:39 on Apr 7, 2016

spongeh
Mar 22, 2009

BREADAGRAM OF PROTECTION

COOL CORN posted:

Does anyone use Go for game development? I'm curious what the most popular use cases of Go are at the moment - I guess mostly web-based?

https://github.com/veandco/go-sdl2 These bindings work very well, but the biggest thing for me was just the lack of debugging tools. printf debugging doesn't cut it for game dev, and there wasn't anything promising that worked and didn't murder performance either. This was 6 months ago, I think I've seen a project or two that I didn't find originally that might be good once it matures, but I eventually went with a Lua based platform for my tinkering (love2d.org)

I did have some basic level loading, tile scrolling working in a plain ol redistributable .exe, so it's possible, and may someday even be a good way to go, but just keep in mind you're essentially writing everything yourself since there's not much of a community for game dev in Go.

bonds0097
Oct 23, 2010

I would cry but I don't think I can spare the moisture.
Pillbug

spongeh posted:

https://github.com/veandco/go-sdl2 These bindings work very well, but the biggest thing for me was just the lack of debugging tools. printf debugging doesn't cut it for game dev, and there wasn't anything promising that worked and didn't murder performance either. This was 6 months ago, I think I've seen a project or two that I didn't find originally that might be good once it matures, but I eventually went with a Lua based platform for my tinkering (love2d.org)

I did have some basic level loading, tile scrolling working in a plain ol redistributable .exe, so it's possible, and may someday even be a good way to go, but just keep in mind you're essentially writing everything yourself since there's not much of a community for game dev in Go.

Have you tried Delve for Go debugging? Just started using it with VS Code. Don't have much to say other than it's a thing but dunno if it's any good.

Bozart
Oct 28, 2006

Give me the finger.
There is also an atom plugin.

spongeh
Mar 22, 2009

BREADAGRAM OF PROTECTION
I think Delve wasn't available for Windows at the time. That's good that it seems to be now, but no I hadn't used it at the time.

Coffee Mugshot
Jun 26, 2010

by Lowtax
Delve is bad now but maybe it will be less bad in the near future.

Adbot
ADBOT LOVES YOU

Ganson
Jul 13, 2007
I know where the electrical tape is!

Suspicious Dish posted:

They still seem to believe that dynamic linking is the devil, so it's hard to write a solid Go library.

Admittedly for most contexts in which I would use golang, dynamic linking IS the devil. Go is a great language to pick for operations code where you would have used python or perl 10 years ago. Back then you would just have had to eat the pain of dependency management for a non-mainstack language (or do something in c and eat the pain of THAT). Now you just ship a static link binary and call it a day.

Ganson fucked around with this message at 23:38 on Apr 17, 2016

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