Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
In this episode Java teacher Craig Dennis sits down with Go instructor Jay McGavren to discuss interfaces and Go in general.
Hey, Jay, I saw that blog post that you did about the Go interfaces. 0:01 >> Cool. 0:06 >> As a Java teacher, I looked at some of the code, and 0:07 some of it looked familiar and some of it didn't. 0:09 You got some time where we could sit down and 0:11 take a quick look at the differences here? 0:13 >> Sure. I know what you're on to here. 0:15 There's a lot of similarities between Go and Java. 0:17 Yeah, let's take a look. 0:20 >> Cool, so this is the workspace that you have up here, right? 0:22 >> That's right. 0:23 >> Okay, so package pets. 0:24 >> Right. 0:27 So, this is packages in Go. 0:28 They're just libraries, basically, bits of reusable code. 0:30 I forget what the Java term is. 0:33 >> Jars. >> Jars, right, yeah, 0:34 roughly equivalent to jars in Java. 0:36 >> And it's in a folder called pets, and that makes it the package? 0:39 >> Yes, that's right. 0:43 But more importantly, it's got this package pets declaration, 0:46 right here at the top. 0:48 >> At the top? 0:49 >> Right. >> Okay, it doesn't matter, 0:50 the folder structure doesn't really matter? 0:51 >> The folder structure helps, the tools that Go uses when compiling 0:52 software rely on that folder structure too. 0:57 >> Cool. 1:00 And then you're importing FMT, which I'm assuming is format? 1:01 >> Which is format. 1:05 This is another package. 1:05 The import statement imports another package, so 1:07 that you can use the things it contains in your current package. 1:10 >> Gotcha. And that's like a base. 1:13 Library FMT is, from Go the language is full. 1:15 >> Yes, yes. 1:17 That is part of the Go standard library. 1:18 So if Go's installed on your computer, the format package is available. 1:21 >> Okay, awesome. 1:25 So, next we have type Dog struct. 1:27 >> Well here, let me run the program real quick here, and 1:31 we'll demonstrate what it does. 1:34 So basically, it creates a couple of objects. 1:36 One is a dog object here. 1:40 Fido walks across the room and sits down. 1:43 And then we've got a cat object here. 1:45 Fluffy walks across the room and sits down. 1:47 >> Okay, cool. 1:51 So first you're defining a type. 1:52 >> Yes, which you can think of as kinda like a class in Java. 1:55 It works a little differently. 2:00 It doesn't map exactly one to one, but- >> Okay. 2:01 And the way that it's not mapping is, 2:04 it looks like you're not defining the method inside. 2:06 >> That's right. 2:11 You actually have your type definition up here, and 2:12 as soon as you run even just this portion, you have a Dog type that you can use. 2:16 It's gonna have a name attribute that you can assign a string to, 2:21 because that's its declared type, a string. 2:25 And it's gonna have a breed type that you can assign a string to. 2:28 >> Instruct is kinda like, that's like the structure? 2:31 Is that what- >> Right, that's a holdover from C. 2:34 There's a lot of similarities between Go and the C programming language. 2:36 And it basically lets you stick a bunch of different fields of different types 2:41 together. 2:46 You can mix strings with integers with whatever else you need. 2:47 >> Can you have a type without a struct, that word there? 2:50 >> Yes, actually. 2:53 I could take a plain old floating point number, and make that, and 2:54 wrap that in a type if I wanted to. 2:58 >> Gotcha, so you're saying, this is a type of Dog that is a structure. 3:01 >> Yes. 3:07 >> Okay, gotcha, cool, awesome. 3:07 I'm assuming that func is function. 3:10 >> Yes. >> And 3:12 now the switchy bit there of the d Dog, what is that doing? 3:13 How are you- >> This is a little interesting. 3:18 This is a bit of ghost syntax that is not typically in other languages. 3:21 So I can declare this a function, 3:27 which then becomes a method on the Dog type. 3:33 I can declare that anywhere in the pets package that I want. 3:36 It could be way down at the bottom of the file. 3:39 It doesn't have to be nested inside this type declaration up here. 3:41 >> But this here is kind of the defining the scope. 3:46 So d is the name of the variable that you could use? 3:50 >> Correct. 3:54 >> Correct, and this is just idiomatic Go. 3:55 It's a style of writing Go that a lot of developers use. 4:00 So I just happen to name my Dog variable d. 4:05 And then I can access that Dog variable here 4:09 within the body of the function, the method. 4:13 >> So it's kinda like setting this. 4:17 >> Yes, exactly, there is no concept of this in Go, 4:19 which I know this is used in Java. 4:23 >> Yeah. You decide what this is called, here. 4:25 >> And that's what that's defining. 4:28 >> Yes. 4:29 The technical term for this is, this is the method receiver. 4:30 This is the object that the method is going to act on. 4:34 >> That makes sense. 4:36 And then, I'm noticing that, so these are method names. 4:37 Are they called methods now? 4:44 Are they called functions still? 4:44 >> All methods are functions. 4:49 Not all functions are methods. 4:50 >> Gotcha. 4:52 So, the methods have a capital letter. 4:53 They start with a capital letter there, 4:56 that's a little bit different than what I'm used to. 4:57 >> Right, that's an important, 4:59 you know how you might declare things to be public in Java? 5:00 In this you have entities that are exported from a package, 5:05 and entities that are un-exported from a package. 5:10 >> Okay, gotcha. 5:13 >> If you wanna do encapsulation, you keep something un-exported. 5:13 And then it can only be accessed by other functions and methods in that package. 5:16 >> Okay, so this makes it exportable? 5:23 >> Naming it with a capital letter as its first letter of its name, that exports it. 5:27 That's the whole syntax right there. 5:31 >> Wow, that saves a lot of- >> A lot of typing. 5:33 And it makes it super obvious, whether something is public or private, so yeah. 5:35 >> Right, interesting. 5:40 I would not have guessed that just by looking at it. 5:42 Thanks for sharing that. 5:45 Okay, so I understand the next one. 5:47 Here's a method that, it's a publicly exported method called Sit. 5:50 >> Yes, called Sit. 5:54 >> And it's gonna take the Dog structure that you passed, and 5:55 it's gonna pull the name out, okay, awesome. 5:59 >> Right. And I'm print line's exported. 6:00 That's what the capital P's about on the format. 6:02 >> Yes, correct. >> Okay, awesome. 6:04 >> Applies even on the format package. 6:05 >> Cool, all right, okay, and now we're gonna come down here, and 6:07 we're gonna do a new type called Cat, which is of type structure. 6:10 It's extending a structure that has a name and a string. 6:13 Okay, awesome. 6:17 And it walks, okay. 6:19 And it sits and it purrs. 6:20 >> Yes, and the interesting thing to note here, between the Cat and the Dog types, 6:21 is that both Dog and Cat have Walk and Sit methods. 6:28 >> The Dog can't purr. 6:33 >> Right. The Dog can't purr and 6:35 the Cat can't fetch, but they can both walk and sit. 6:36 >> Okay, okay, awesome. 6:40 So now, in my world, I would say that that sounds like there's an interface, 6:42 and that's what your article is about. 6:48 >> Absolutely. >> So let's take a look at that. 6:49 >> And Go interfaces accomplish many of the same things that Java interfaces do. 6:52 What's awesome about them is you can declare interfaces anywhere. 6:57 You don't have to be the owner of the jar, 7:01 the package, that the classes were originally declared in. 7:05 I wrote this demo.go file. 7:12 It's totally outside the pets package. 7:15 You'll notice it's part of package main. 7:17 And here is where I declare my FourLegged interface. 7:19 I don't have to go in and touch those Dog or Cat classes. 7:23 Right, I just declare that anything, any type that happens to have Walk and 7:25 Sit methods satisfies the FourLegged interface. 7:33 >> Gotcha, so that's like duck typing in Python or Ruby, 7:38 I think Ruby called it duck typing. 7:40 >> Yes, it's duck typing at compile time. 7:42 >> That's awesome. 7:45 >> Yeah this is super cool, and this part is unique to Go. 7:46 >> Okay, awesome. 7:50 That's really neat. 7:51 So now, you basically have a type. 7:51 So that's what you're saying here, is that this takes a FourLegged type, and 7:54 that type is one that you just declared here, with an interface. 7:58 >> Yes, basically. 8:02 So, my demo function here, and this is not a method, by the way. 8:04 You'll notice it doesn't have a receiver over here. 8:08 My demo function here takes a FourLegged object, 8:12 something of the FourLegged type. 8:16 And that can be of any type that happens to have Walk and Sit methods. 8:20 >> Walk and Sit. 8:25 Which you define right there on line five. 8:26 >> Yes, yes. 8:28 So I define the interface up here, and I use it down here. 8:29 Because whatever value I've passed in is of the FourLegged type, 8:32 I know for sure that it has the Walk and Sit methods. 8:37 Otherwise it wouldn't have compiled. 8:41 So I could call Walk and Sit. 8:43 >> And here's your main method which calls, just like Java, the main method. 8:45 >> Exactly, it runs automatically when the program runs. 8:49 So I create a Dog and a Cat. 8:52 A Dog named Fido with a breed of Terrier, 8:54 a Cat named Fluffy with a breed of Siamese. 8:57 >> What's the little face thing, that little :=, what's that? 8:59 >> That is a short variable declaration. 9:02 So this is going to be equivalent to if I said var dog, Dog. 9:06 >> It's the type, it's doing type inference. 9:13 >> Right, right, exactly, and yeah. 9:16 >> That's coming soon to Java, we're getting that soon. 9:18 >> Cool, cool. 9:21 >> It's exciting, yeah. 9:21 >> And yeah, same thing here for the Cat, it creates a variable. 9:22 Because this code here creates a value of type Cat, 9:27 this will create a variable that holds type Cat. 9:30 >> Awesome, and then you're calling the demo method which takes FourLegged, 9:34 which is the interface that you created Awesome. 9:37 >> It can take either a Dog or a Cat, because both of those types satisfy 9:39 the FourLegged interface that we declared up there. 9:42 >> Thanks, I was reading your blog post looking at the code, didn't fully get it. 9:45 I totally understand the power of this now. 9:50 This is awesome, man. 9:51 Thank you for taking the time at lunch to do this with me. 9:53 >> My pleasure. 9:55 [MUSIC] 9:56
You need to sign up for Treehouse in order to download course files.Sign up