1 00:00:01,770 --> 00:00:06,740 Hey, Jay, I saw that blog post that you did about the Go interfaces. 2 00:00:06,740 --> 00:00:07,470 >> Cool. 3 00:00:07,470 --> 00:00:09,760 >> As a Java teacher, I looked at some of the code, and 4 00:00:09,760 --> 00:00:11,770 some of it looked familiar and some of it didn't. 5 00:00:11,770 --> 00:00:13,890 You got some time where we could sit down and 6 00:00:13,890 --> 00:00:15,940 take a quick look at the differences here? 7 00:00:15,940 --> 00:00:17,390 >> Sure. I know what you're on to here. 8 00:00:17,390 --> 00:00:20,770 There's a lot of similarities between Go and Java. 9 00:00:20,770 --> 00:00:22,110 Yeah, let's take a look. 10 00:00:22,110 --> 00:00:23,529 >> Cool, so this is the workspace that you have up here, right? 11 00:00:23,529 --> 00:00:24,178 >> That's right. 12 00:00:24,178 --> 00:00:27,080 >> Okay, so package pets. 13 00:00:27,080 --> 00:00:28,045 >> Right. 14 00:00:28,045 --> 00:00:30,110 So, this is packages in Go. 15 00:00:30,110 --> 00:00:33,320 They're just libraries, basically, bits of reusable code. 16 00:00:33,320 --> 00:00:34,616 I forget what the Java term is. 17 00:00:34,616 --> 00:00:36,262 >> Jars. >> Jars, right, yeah, 18 00:00:36,262 --> 00:00:39,335 roughly equivalent to jars in Java. 19 00:00:39,335 --> 00:00:43,440 >> And it's in a folder called pets, and that makes it the package? 20 00:00:43,440 --> 00:00:44,170 >> Yes, that's right. 21 00:00:46,110 --> 00:00:48,820 But more importantly, it's got this package pets declaration, 22 00:00:48,820 --> 00:00:49,480 right here at the top. 23 00:00:49,480 --> 00:00:50,020 >> At the top? 24 00:00:50,020 --> 00:00:51,020 >> Right. >> Okay, it doesn't matter, 25 00:00:51,020 --> 00:00:52,830 the folder structure doesn't really matter? 26 00:00:52,830 --> 00:00:57,830 >> The folder structure helps, the tools that Go uses when compiling 27 00:00:57,830 --> 00:01:00,660 software rely on that folder structure too. 28 00:01:00,660 --> 00:01:01,340 >> Cool. 29 00:01:01,340 --> 00:01:05,060 And then you're importing FMT, which I'm assuming is format? 30 00:01:05,060 --> 00:01:05,790 >> Which is format. 31 00:01:05,790 --> 00:01:07,120 This is another package. 32 00:01:07,120 --> 00:01:10,019 The import statement imports another package, so 33 00:01:10,019 --> 00:01:13,290 that you can use the things it contains in your current package. 34 00:01:13,290 --> 00:01:15,160 >> Gotcha. And that's like a base. 35 00:01:15,160 --> 00:01:17,740 Library FMT is, from Go the language is full. 36 00:01:17,740 --> 00:01:18,850 >> Yes, yes. 37 00:01:18,850 --> 00:01:21,698 That is part of the Go standard library. 38 00:01:21,698 --> 00:01:25,930 So if Go's installed on your computer, the format package is available. 39 00:01:25,930 --> 00:01:27,587 >> Okay, awesome. 40 00:01:27,587 --> 00:01:31,670 So, next we have type Dog struct. 41 00:01:31,670 --> 00:01:34,625 >> Well here, let me run the program real quick here, and 42 00:01:34,625 --> 00:01:36,495 we'll demonstrate what it does. 43 00:01:36,495 --> 00:01:40,080 So basically, it creates a couple of objects. 44 00:01:40,080 --> 00:01:43,085 One is a dog object here. 45 00:01:43,085 --> 00:01:45,780 Fido walks across the room and sits down. 46 00:01:45,780 --> 00:01:47,759 And then we've got a cat object here. 47 00:01:47,759 --> 00:01:51,026 Fluffy walks across the room and sits down. 48 00:01:51,026 --> 00:01:52,010 >> Okay, cool. 49 00:01:52,010 --> 00:01:55,200 So first you're defining a type. 50 00:01:55,200 --> 00:02:00,205 >> Yes, which you can think of as kinda like a class in Java. 51 00:02:00,205 --> 00:02:01,775 It works a little differently. 52 00:02:01,775 --> 00:02:04,200 It doesn't map exactly one to one, but- >> Okay. 53 00:02:04,200 --> 00:02:06,605 And the way that it's not mapping is, 54 00:02:06,605 --> 00:02:11,870 it looks like you're not defining the method inside. 55 00:02:11,870 --> 00:02:12,880 >> That's right. 56 00:02:12,880 --> 00:02:16,478 You actually have your type definition up here, and 57 00:02:16,478 --> 00:02:21,809 as soon as you run even just this portion, you have a Dog type that you can use. 58 00:02:21,809 --> 00:02:25,660 It's gonna have a name attribute that you can assign a string to, 59 00:02:25,660 --> 00:02:28,473 because that's its declared type, a string. 60 00:02:28,473 --> 00:02:31,280 And it's gonna have a breed type that you can assign a string to. 61 00:02:31,280 --> 00:02:34,230 >> Instruct is kinda like, that's like the structure? 62 00:02:34,230 --> 00:02:36,760 Is that what- >> Right, that's a holdover from C. 63 00:02:36,760 --> 00:02:39,950 There's a lot of similarities between Go and the C programming language. 64 00:02:41,570 --> 00:02:46,479 And it basically lets you stick a bunch of different fields of different types 65 00:02:46,479 --> 00:02:47,244 together. 66 00:02:47,244 --> 00:02:50,842 You can mix strings with integers with whatever else you need. 67 00:02:50,842 --> 00:02:53,880 >> Can you have a type without a struct, that word there? 68 00:02:53,880 --> 00:02:54,790 >> Yes, actually. 69 00:02:54,790 --> 00:02:58,991 I could take a plain old floating point number, and make that, and 70 00:02:58,991 --> 00:03:01,101 wrap that in a type if I wanted to. 71 00:03:01,101 --> 00:03:07,240 >> Gotcha, so you're saying, this is a type of Dog that is a structure. 72 00:03:07,240 --> 00:03:07,810 >> Yes. 73 00:03:07,810 --> 00:03:10,310 >> Okay, gotcha, cool, awesome. 74 00:03:10,310 --> 00:03:12,110 I'm assuming that func is function. 75 00:03:12,110 --> 00:03:13,352 >> Yes. >> And 76 00:03:13,352 --> 00:03:18,860 now the switchy bit there of the d Dog, what is that doing? 77 00:03:18,860 --> 00:03:21,470 How are you- >> This is a little interesting. 78 00:03:21,470 --> 00:03:27,564 This is a bit of ghost syntax that is not typically in other languages. 79 00:03:27,564 --> 00:03:33,700 So I can declare this a function, 80 00:03:33,700 --> 00:03:36,370 which then becomes a method on the Dog type. 81 00:03:36,370 --> 00:03:39,080 I can declare that anywhere in the pets package that I want. 82 00:03:39,080 --> 00:03:41,280 It could be way down at the bottom of the file. 83 00:03:41,280 --> 00:03:46,155 It doesn't have to be nested inside this type declaration up here. 84 00:03:46,155 --> 00:03:50,097 >> But this here is kind of the defining the scope. 85 00:03:50,097 --> 00:03:54,699 So d is the name of the variable that you could use? 86 00:03:54,699 --> 00:03:55,320 >> Correct. 87 00:03:55,320 --> 00:03:59,320 >> Correct, and this is just idiomatic Go. 88 00:04:00,830 --> 00:04:05,770 It's a style of writing Go that a lot of developers use. 89 00:04:05,770 --> 00:04:08,340 So I just happen to name my Dog variable d. 90 00:04:09,550 --> 00:04:13,188 And then I can access that Dog variable here 91 00:04:13,188 --> 00:04:17,143 within the body of the function, the method. 92 00:04:17,143 --> 00:04:19,630 >> So it's kinda like setting this. 93 00:04:19,630 --> 00:04:23,500 >> Yes, exactly, there is no concept of this in Go, 94 00:04:23,500 --> 00:04:25,250 which I know this is used in Java. 95 00:04:25,250 --> 00:04:28,330 >> Yeah. You decide what this is called, here. 96 00:04:28,330 --> 00:04:29,996 >> And that's what that's defining. 97 00:04:29,996 --> 00:04:30,590 >> Yes. 98 00:04:30,590 --> 00:04:34,350 The technical term for this is, this is the method receiver. 99 00:04:34,350 --> 00:04:36,790 This is the object that the method is going to act on. 100 00:04:36,790 --> 00:04:37,720 >> That makes sense. 101 00:04:37,720 --> 00:04:44,376 And then, I'm noticing that, so these are method names. 102 00:04:44,376 --> 00:04:44,980 Are they called methods now? 103 00:04:44,980 --> 00:04:48,040 Are they called functions still? 104 00:04:49,320 --> 00:04:50,790 >> All methods are functions. 105 00:04:50,790 --> 00:04:52,460 Not all functions are methods. 106 00:04:52,460 --> 00:04:53,600 >> Gotcha. 107 00:04:53,600 --> 00:04:56,110 So, the methods have a capital letter. 108 00:04:56,110 --> 00:04:57,349 They start with a capital letter there, 109 00:04:57,349 --> 00:04:59,030 that's a little bit different than what I'm used to. 110 00:04:59,030 --> 00:05:00,800 >> Right, that's an important, 111 00:05:00,800 --> 00:05:04,020 you know how you might declare things to be public in Java? 112 00:05:05,160 --> 00:05:10,510 In this you have entities that are exported from a package, 113 00:05:10,510 --> 00:05:13,080 and entities that are un-exported from a package. 114 00:05:13,080 --> 00:05:13,980 >> Okay, gotcha. 115 00:05:13,980 --> 00:05:16,797 >> If you wanna do encapsulation, you keep something un-exported. 116 00:05:16,797 --> 00:05:23,814 And then it can only be accessed by other functions and methods in that package. 117 00:05:23,814 --> 00:05:26,340 >> Okay, so this makes it exportable? 118 00:05:27,340 --> 00:05:31,890 >> Naming it with a capital letter as its first letter of its name, that exports it. 119 00:05:31,890 --> 00:05:33,670 That's the whole syntax right there. 120 00:05:33,670 --> 00:05:35,700 >> Wow, that saves a lot of- >> A lot of typing. 121 00:05:35,700 --> 00:05:40,590 And it makes it super obvious, whether something is public or private, so yeah. 122 00:05:40,590 --> 00:05:42,500 >> Right, interesting. 123 00:05:42,500 --> 00:05:45,370 I would not have guessed that just by looking at it. 124 00:05:45,370 --> 00:05:46,320 Thanks for sharing that. 125 00:05:47,600 --> 00:05:50,130 Okay, so I understand the next one. 126 00:05:50,130 --> 00:05:53,370 Here's a method that, it's a publicly exported method called Sit. 127 00:05:54,490 --> 00:05:55,960 >> Yes, called Sit. 128 00:05:55,960 --> 00:05:59,430 >> And it's gonna take the Dog structure that you passed, and 129 00:05:59,430 --> 00:06:00,910 it's gonna pull the name out, okay, awesome. 130 00:06:00,910 --> 00:06:02,710 >> Right. And I'm print line's exported. 131 00:06:02,710 --> 00:06:04,742 That's what the capital P's about on the format. 132 00:06:04,742 --> 00:06:05,950 >> Yes, correct. >> Okay, awesome. 133 00:06:05,950 --> 00:06:07,980 >> Applies even on the format package. 134 00:06:07,980 --> 00:06:10,860 >> Cool, all right, okay, and now we're gonna come down here, and 135 00:06:10,860 --> 00:06:13,925 we're gonna do a new type called Cat, which is of type structure. 136 00:06:13,925 --> 00:06:17,520 It's extending a structure that has a name and a string. 137 00:06:17,520 --> 00:06:19,090 Okay, awesome. 138 00:06:19,090 --> 00:06:20,097 And it walks, okay. 139 00:06:20,097 --> 00:06:21,740 And it sits and it purrs. 140 00:06:21,740 --> 00:06:28,230 >> Yes, and the interesting thing to note here, between the Cat and the Dog types, 141 00:06:28,230 --> 00:06:33,890 is that both Dog and Cat have Walk and Sit methods. 142 00:06:33,890 --> 00:06:35,130 >> The Dog can't purr. 143 00:06:35,130 --> 00:06:36,910 >> Right. The Dog can't purr and 144 00:06:36,910 --> 00:06:40,610 the Cat can't fetch, but they can both walk and sit. 145 00:06:40,610 --> 00:06:42,520 >> Okay, okay, awesome. 146 00:06:42,520 --> 00:06:48,070 So now, in my world, I would say that that sounds like there's an interface, 147 00:06:48,070 --> 00:06:49,540 and that's what your article is about. 148 00:06:49,540 --> 00:06:52,791 >> Absolutely. >> So let's take a look at that. 149 00:06:52,791 --> 00:06:57,550 >> And Go interfaces accomplish many of the same things that Java interfaces do. 150 00:06:57,550 --> 00:07:01,780 What's awesome about them is you can declare interfaces anywhere. 151 00:07:01,780 --> 00:07:05,778 You don't have to be the owner of the jar, 152 00:07:05,778 --> 00:07:12,526 the package, that the classes were originally declared in. 153 00:07:12,526 --> 00:07:15,721 I wrote this demo.go file. 154 00:07:15,721 --> 00:07:17,660 It's totally outside the pets package. 155 00:07:17,660 --> 00:07:19,890 You'll notice it's part of package main. 156 00:07:19,890 --> 00:07:23,080 And here is where I declare my FourLegged interface. 157 00:07:23,080 --> 00:07:25,690 I don't have to go in and touch those Dog or Cat classes. 158 00:07:25,690 --> 00:07:33,257 Right, I just declare that anything, any type that happens to have Walk and 159 00:07:33,257 --> 00:07:38,360 Sit methods satisfies the FourLegged interface. 160 00:07:38,360 --> 00:07:40,687 >> Gotcha, so that's like duck typing in Python or Ruby, 161 00:07:40,687 --> 00:07:42,640 I think Ruby called it duck typing. 162 00:07:42,640 --> 00:07:45,690 >> Yes, it's duck typing at compile time. 163 00:07:45,690 --> 00:07:46,320 >> That's awesome. 164 00:07:46,320 --> 00:07:50,115 >> Yeah this is super cool, and this part is unique to Go. 165 00:07:50,115 --> 00:07:51,250 >> Okay, awesome. 166 00:07:51,250 --> 00:07:51,810 That's really neat. 167 00:07:51,810 --> 00:07:54,263 So now, you basically have a type. 168 00:07:54,263 --> 00:07:58,867 So that's what you're saying here, is that this takes a FourLegged type, and 169 00:07:58,867 --> 00:08:02,662 that type is one that you just declared here, with an interface. 170 00:08:02,662 --> 00:08:04,056 >> Yes, basically. 171 00:08:04,056 --> 00:08:08,870 So, my demo function here, and this is not a method, by the way. 172 00:08:08,870 --> 00:08:12,140 You'll notice it doesn't have a receiver over here. 173 00:08:12,140 --> 00:08:16,939 My demo function here takes a FourLegged object, 174 00:08:16,939 --> 00:08:20,376 something of the FourLegged type. 175 00:08:20,376 --> 00:08:25,120 And that can be of any type that happens to have Walk and Sit methods. 176 00:08:25,120 --> 00:08:26,080 >> Walk and Sit. 177 00:08:26,080 --> 00:08:28,350 Which you define right there on line five. 178 00:08:28,350 --> 00:08:29,240 >> Yes, yes. 179 00:08:29,240 --> 00:08:32,530 So I define the interface up here, and I use it down here. 180 00:08:32,530 --> 00:08:37,389 Because whatever value I've passed in is of the FourLegged type, 181 00:08:37,389 --> 00:08:41,131 I know for sure that it has the Walk and Sit methods. 182 00:08:41,131 --> 00:08:43,690 Otherwise it wouldn't have compiled. 183 00:08:43,690 --> 00:08:45,300 So I could call Walk and Sit. 184 00:08:45,300 --> 00:08:49,480 >> And here's your main method which calls, just like Java, the main method. 185 00:08:49,480 --> 00:08:52,420 >> Exactly, it runs automatically when the program runs. 186 00:08:52,420 --> 00:08:54,130 So I create a Dog and a Cat. 187 00:08:54,130 --> 00:08:57,170 A Dog named Fido with a breed of Terrier, 188 00:08:57,170 --> 00:08:59,730 a Cat named Fluffy with a breed of Siamese. 189 00:08:59,730 --> 00:09:02,894 >> What's the little face thing, that little :=, what's that? 190 00:09:02,894 --> 00:09:06,640 >> That is a short variable declaration. 191 00:09:06,640 --> 00:09:13,954 So this is going to be equivalent to if I said var dog, Dog. 192 00:09:13,954 --> 00:09:16,680 >> It's the type, it's doing type inference. 193 00:09:16,680 --> 00:09:18,913 >> Right, right, exactly, and yeah. 194 00:09:18,913 --> 00:09:21,166 >> That's coming soon to Java, we're getting that soon. 195 00:09:21,166 --> 00:09:21,853 >> Cool, cool. 196 00:09:21,853 --> 00:09:22,680 >> It's exciting, yeah. 197 00:09:22,680 --> 00:09:27,267 >> And yeah, same thing here for the Cat, it creates a variable. 198 00:09:27,267 --> 00:09:30,932 Because this code here creates a value of type Cat, 199 00:09:30,932 --> 00:09:34,738 this will create a variable that holds type Cat. 200 00:09:34,738 --> 00:09:37,260 >> Awesome, and then you're calling the demo method which takes FourLegged, 201 00:09:37,260 --> 00:09:39,625 which is the interface that you created Awesome. 202 00:09:39,625 --> 00:09:42,834 >> It can take either a Dog or a Cat, because both of those types satisfy 203 00:09:42,834 --> 00:09:45,385 the FourLegged interface that we declared up there. 204 00:09:45,385 --> 00:09:50,245 >> Thanks, I was reading your blog post looking at the code, didn't fully get it. 205 00:09:50,245 --> 00:09:51,391 I totally understand the power of this now. 206 00:09:51,391 --> 00:09:53,075 This is awesome, man. 207 00:09:53,075 --> 00:09:55,676 Thank you for taking the time at lunch to do this with me. 208 00:09:55,676 --> 00:09:56,535 >> My pleasure. 209 00:09:56,535 --> 00:10:00,019 [MUSIC]