text
stringlengths
0
2.35k
**Jerod Santo:** So with the quick assignment you do not have to explicitly declare the type of the variable.
**Kris Brandow:** \[16:02\] Yeah. It also gives the language more of a feel of like a dynamically typed language as well... Because there are some specific things in Go that are kind of type-ambiguous until you say what the type is. Constant numbers are an example of this; they're the special number type. The language ...
**Jerod Santo:** Okay. So coming from dynamic languages mostly, I would just wanna use quick assignment all the time, because I would always wanna defer that until later. But I don't see it used all the time. So there are times where var or the equals, the non-quick, the slow assignment is just preferable. Maybe you ju...
**Kris Brandow:** Yeah. I might be wrong about this, but I don't think you can use the quick assignment outside of a function. So if you're just like in the global space, you have to use var.
**Jerod Santo:** I think I hit that, and I wondered why.
**Kris Brandow:** And then also, if you're trying to assign something to a property of a struct, you can't use quick assignment; even if you're doing multiple assignment, you have to use regular equals there. So you kind of have to declare the type -- you have to use var and declare types for that.
I would also say that a lot of the time when I don't use quick assignment, it's because I want the type there for documentation purposes; so leaning more into the -- knowing the type of this upfront makes it easier for people to read and understand "Oh, this is going to be this specific thing", instead of something mor...
**Ian Lopshire:** I kind of think of it as creating the variable, then assigning it. So if you're inside a new scope, the quick assignment will shadow variables outside of that scope. So it's really redefining the variable, and then assigning.
**Jerod Santo:** Say that again in other words. I think I'm with you, but I want you to say it in other words.
**Ian Lopshire:** So say I'm writing a closure, right? Or I have something coming in.
**Jerod Santo:** Right.
**Ian Lopshire:** If I do quick assignment inside of that, like the next scope, it's gonna shadow the variable. That was a horrible example.
**Jerod Santo:** It's gonna overwrite what was outside of the closure.
**Ian Lopshire:** It's not gonna overwrite it, it's going to define a new variable, and inside of that scope you can only reference...
**Jerod Santo:** The new one.
**Ian Lopshire:** The new one.
**Jerod Santo:** Oh, that's not good.
**Ian Lopshire:** I mean, it's desirable sometimes, but if you use the equal sign, it's gonna set the value outside of the scope.
**Jerod Santo:** Okay. When is shadowing desirable? This might be tough, because you might have pre-thought of an example... But when would shadowing be desirable? To me it sounds like just a cesspool for bugs.
**Ian Lopshire:** So this is kind of a weird case, but inside of a for loop sometimes you do wanna shadow, because the way the for loops work is it'll save the reference outside of the loop. So as the loop goes through, your variable is gonna change. But if you do a quick inside of it, you can copy the value into the n...
**Jerod Santo:** Okay.
**Ian Lopshire:** Do you know how to say that better, Kris? I don't know how to say it without showing a picture.
**Jerod Santo:** I think I tracked it, but yeah, Kris, go ahead.
**Kris Brandow:** Yeah, it's basically -- you know, if you have a good variable name and you want to reuse it, but you don't wanna affect things outside of the loop or the function that you're using...
**Jerod Santo:** Okay... That's a good way of saying it. So you're appealing to my desire not to think of another name now.
**Ian Lopshire:** You'll see i=i a lot.
**Jerod Santo:** I see.
**Ian Lopshire:** Like, i=, :=.
**Jerod Santo:** Gotcha.
**Kris Brandow:** The thing that I think a lot of new people run into when they first start using goroutines and loops - if when you get into the loop you don't reassign the variable to something that is more locally scoped to that, and you try to use that inside of the goroutine, all of the goroutines will basically s...
**Jerod Santo:** Gotcha.
**Kris Brandow:** \[20:07\] So there are some uses around that. This always burns me when I'm doing unit testing; that's one of the areas where I start using goroutines and closures, and I'm like "Why isn't this working properly?" It's like, "Oh, right, I have to do this t:=t sort of thing" in order to redefine that va...
There are other ways to handle that as well, that are a bit more explicit, but that's definitely where I think it gets -- shadowing is used the most often to make it so that it's not a bug, and actually something that's helpful to you.
**Jerod Santo:** Yeah. So it sounds like the implicit nature there seems like almost not a Go way of doing things, but I understand for (I guess) syntactic simplicity. But the implicit... Maybe it's an idiom that once you get into Go, you're just like -- everybody knows "Oh, this is a quick assignment, and so this is p...
**Kris Brandow:** Quick assignment is. I mean, you could technically do shadowing with either, but the quick assignment is where people usually get tripped up.
**Jerod Santo:** So when you guys who are seasoned Gophers see a quick assignment inside of a for loop, for instance, or maybe with goroutines, you just immediately know what's going on there, you're like "Oh, I know", because it's i and i, or whatever, why that's there?
**Kris Brandow:** Yeah. There's sometimes where it's just like -- if I'm looking at some code and all of a sudden I see... Especially in testing, when it's t:=t, I'll be like "Oh yeah, that has to be there." Whereas I think other newer people would be like "What's that doing? I don't understand."
**Jerod Santo:** Right, right, right.
**Kris Brandow:** So anyways when I see that. But that doesn't make us immune from shadowing bugs. I've written my fair amount of shadowing bugs where I've spent a lot of time debugging and being like "Why is this error nil?" And it's like, "Oh, because I went into this different scope and I reassigned err, so in the l...
**Ian Lopshire:** Yeah, I think especially in error handling shadowing happens a lot, and it's just part of the language, really.
**Jerod Santo:** Part of the language. Fair enough. So by the way, we have people in the \#GoTimeFM chat sounding off a little bit with some definitions and whatnot, so that's cool. Dillan Bork says "Array is fixed size list of items. Slice is a "pointer" to some subset of an array." So there's a nice, simple explainer...
If you're not hanging out in the \#GoTimeFM channel, what is wrong?! We're all hanging out in there live during the show; you can participate and be part of the fun. So hop into \#GoTimeFM of the Gopher Slack during our shows.
Okay, let's go on to a bigger question. These are kind of small language things. What's the state of the art on dependency management? How do I use other people's code in the idiomatic, best way today? I know there's like a history here, so I'm not ignorant to any of the history, but I just wanna know, just starting to...
**Ian Lopshire:** I think if you're starting today, a hundred percent embrace modules and just use it. It works well, especially for new projects.
**Jerod Santo:** Okay. Is that what everyone is doing?
**Kris Brandow:** Yeah, modules is the way (I guess) of the future for right now...
**Jerod Santo:** The way of the future... \[laughs\] Okay.
**Kris Brandow:** So if you're getting started with a brand new project -- I mean, for all my new stuff, I use modules as well. So yeah, it's just like, okay, get some modules... All of the commands are the same as they used to be; so you still use go get to get dependencies, and all that sort of stuff.
**Jerod Santo:** Right.
**Kris Brandow:** But yeah, when you open up a new folder, run go.mod in it with the name of the project, what you want to name your module or your project... And then you can start just doing go get whatever to go download some stuff, and off to the races.