Latest / CyberSecurity Summary / Mastering Go: Harness the power of Go to build professional utilities and concurrent servers and services
Transcript
- 0:00Speaker 1: Welcome to the deep dive. This is where we take
- 0:03Speaker 1: you know, that big stack of technical papers, research, maybe
- 0:06Speaker 1: some book chapters, and boil it down. Today we're digging
- 0:10Speaker 1: into mastering Go third edition. We're focusing on the Go
- 0:14Speaker 1: programming language, Go laying right.
- 0:16Speaker 2: And our mission today it's pretty straightforward. We want to
- 0:19Speaker 2: give you the core ideas, the sort of architectural thinking
- 0:23Speaker 2: behind Go.
- 0:24Speaker 1: Yeah, what makes it good for system stuff concurrency exactly?
- 0:27Speaker 2: And how you can grasp that without wading through like
- 0:30Speaker 2: super dense text. We want to get the feel of.
- 0:33Speaker 1: Go, the mindset. Yeah. The source material really hammers home
- 0:36Speaker 1: this idea that goes strength comes from being well, simple
- 0:41Speaker 1: and clear ruthlessly so almost it really does. So let's
- 0:44Speaker 1: unpack that. Where did it start? What's its main goal?
- 0:46Speaker 2: Okay, so Go it's open source, started at Google and
- 0:49Speaker 2: went public around two thousand and nine.
- 0:51Speaker 1: And the names involved Greasemur, Thompson, Pike. These aren't just
- 0:54Speaker 1: random folks.
- 0:55Speaker 2: No, absolutely not. These are like programming legends. They knew
- 0:59Speaker 2: what systems programming needed, especially with multi core processors becoming
- 1:03Speaker 2: the norm. They were building for reliability, for scale.
- 1:06Speaker 1: And the source emphasizes it's mainly for systems programming, right,
- 1:11Speaker 1: even though it's general.
- 1:12Speaker 2: Purpose, that's its sweet spot. Think system tools, command line utilities, Docker, Hugo,
- 1:18Speaker 2: those kinds of things.
- 1:19Speaker 1: Big infrastructure too, like Kubernetes, a STO, massive networking servers definitely.
- 1:24Speaker 2: And that efficiency thinking. It starts right away with how
- 1:27Speaker 2: you actually run your GO code.
- 1:29Speaker 1: Ah, right, you got the two main ways.
- 1:31Speaker 2: Yeah, there's Go run, which is quick, feels kind of
- 1:34Speaker 2: like scripting, you know, builds a temporary thing, runs, it
- 1:38Speaker 2: cleans up, instant feedback.
- 1:40Speaker 1: Good for testing stuff out for sure.
- 1:42Speaker 2: But the real power, especially for systems deployment, is Go build.
- 1:46Speaker 1: And what's the key takeaway there. It's not just compiling, No.
- 1:49Speaker 2: It's what you get a single statically linked binary file
- 1:53Speaker 2: that's huge, meaning everything it needs to run is baked
- 1:57Speaker 2: into that one file. No external dependencies to chase down,
- 2:00Speaker 2: no complex run times to install on the server.
- 2:03Speaker 1: Just copy and run. Solves a lot of deployment etics,
- 2:05Speaker 1: copendency hell gone.
- 2:07Speaker 2: Exactly, total simplicity, which interestingly is why the material even
- 2:11Speaker 2: suggests Go could be a good first language for someone
- 2:15Speaker 2: learning to code because.
- 2:16Speaker 1: It forces good habits, less for both clear rules.
- 2:20Speaker 2: Yeah, it sort of guides you towards clarity right from
- 2:22Speaker 2: the start.
- 2:23Speaker 1: Okay, So this ruthless simplicity idea, it shows up in
- 2:26Speaker 1: the language rules themselves, doesn't it. It's not just philosophy,
- 2:29Speaker 1: it's enforced.
- 2:30Speaker 2: Oh absolutely, And that strictness is what enables the stability
- 2:34Speaker 2: you need for goes big party trick concurrency.
- 2:38Speaker 1: But yeah, the rules like curly braces, that's.
- 2:40Speaker 2: A classic one. The source reminds us there's only one
- 2:43Speaker 2: way to format curly braces and go the tools enforce.
- 2:46Speaker 1: It, which probably saves countless hours of team arguments about
- 2:50Speaker 1: style guides.
- 2:51Speaker 2: You bet. It just settles the debate productivity win right there.
- 2:54Speaker 1: And the other big one is types right, type safety,
- 2:57Speaker 1: very strict.
- 2:58Speaker 2: GO won't just automatically change say an integer into a
- 3:02Speaker 2: float for you, no implicit conversions like that.
- 3:04Speaker 1: You have to explicitly cast it, tell GO yes, I
- 3:07Speaker 1: mean to do.
- 3:08Speaker 2: This exactly, which cuts out a whole category of really
- 3:12Speaker 2: subtle bugs, you know, where the language silently changes the
- 3:15Speaker 2: value and maybe loses precision and you didn't even realize.
- 3:19Speaker 1: Okay, makes sense. Let's talk about function returns, go off
- 3:22Speaker 1: and returns multiple values. How does that work in practice?
- 3:26Speaker 1: Doesn't it get messy.
- 3:27Speaker 2: It actually pushes you towards cleaner error handling. The common
- 3:32Speaker 2: pattern is to return two things, the actual result you
- 3:35Speaker 2: wanted and an.
- 3:36Speaker 1: Error value, like the strack on v dot oi function
- 3:39Speaker 1: for converting strings to integers.
- 3:41Speaker 2: Precisely, it gives you the integer or an error if
- 3:44Speaker 2: the string wasn't a valid number, and you have to
- 3:46Speaker 2: deal with that error variable or the compiler complaint.
- 3:48Speaker 1: So you can't easily ignore potential problems.
- 3:50Speaker 2: Right, and the authors give a nice rule of thumb.
- 3:53Speaker 2: If your function needs to send back more than say,
- 3:55Speaker 2: three values, maybe rethink it. Also bundle them up, return
- 3:59Speaker 2: a customs strike to containing those values, or maybe a slice.
- 4:02Speaker 2: Keep the function's signature clean.
- 4:04Speaker 1: Got it, okay? Data structures arrays versus slices. What's the
- 4:07Speaker 1: core difference we need to grasp?
- 4:09Speaker 2: So arrays in go have a fixed length. Once you
- 4:12Speaker 2: declare an array of five integers, it's always five integers.
- 4:15Speaker 2: Slices are built on top of arrays.
- 4:18Speaker 1: But they're more flexible, much more.
- 4:20Speaker 2: A slice is like a dynamic view or window onto
- 4:24Speaker 2: an underlying array. It keeps track of its length and
- 4:27Speaker 2: its capacity, how much space is available in that underlying array.
- 4:31Speaker 2: You can check capacity with cap and you can add
- 4:33Speaker 2: things yep using the pen function. If you append something
- 4:36Speaker 2: and the underlying array is full, GO handles it for you.
- 4:39Speaker 2: It allocates a new, bigger array and copies everything over.
- 4:43Speaker 2: It's pretty efficient.
- 4:44Speaker 1: So slices are generally what you'd use.
- 4:46Speaker 2: Day to day most of the time. Yeah, they're the workhorse.
- 4:49Speaker 1: Then there are maps key value stores. What's special about
- 4:52Speaker 1: them in Go?
- 4:52Speaker 2: Well, they're powerful because the keys don't have to be integers.
- 4:55Speaker 2: They can be any comparable type, strings, numbers, strucks if
- 4:59Speaker 2: they're ca very flexible indexing.
- 5:01Speaker 1: Okay, but here's something from the notes that seems odd.
- 5:05Speaker 1: Go is about predictability, but map iteration order is randomized.
- 5:10Speaker 2: Ah. Yes, that often surprises people. It's deliberate, though.
- 5:14Speaker 1: Why would they force randomness?
- 5:16Speaker 2: It gets right to the core philosophy. The Go designers
- 5:19Speaker 2: did that specifically to stop developers from accidentally relying on
- 5:23Speaker 2: whatever internal ordering the map happened.
- 5:25Speaker 1: To have at the time, because that internal order could
- 5:28Speaker 1: change between GO versions or even between runs.
- 5:31Speaker 2: Exactly. It's fragile and implementation detail you shouldn't depend on.
- 5:36Speaker 2: By randomizing it, Go forces you if you need a
- 5:39Speaker 2: specific order, you must get the keys, sort them explicitly,
- 5:43Speaker 2: and then.
- 5:43Speaker 1: Iterate right correct code. Don't rely on side effects precisely.
- 5:47Speaker 2: It makes your code more robust.
- 5:48Speaker 1: Okay, that makes a weird kind of sense now, and
- 5:51Speaker 1: a quick warning from the source. Don't try to write
- 5:54Speaker 1: to a map that's mil right.
- 5:55Speaker 2: A nil map is basically an uninitialized map. If you
- 5:58Speaker 2: try to put data into it, your program will panic crash.
- 6:01Speaker 1: You have to initialize it first, usually using make.
- 6:04Speaker 2: Yep, my map, make map. Then you're good to go.
- 6:06Speaker 1: All right, let's get to the main event. For many people, concurrency.
- 6:11Speaker 1: This is where Go really shines, isn't it.
- 6:13Speaker 2: It really is. It's baked into the language design with
- 6:16Speaker 2: two core concepts, gord utines and channel groutines.
- 6:20Speaker 1: What are they?
- 6:20Speaker 2: Fundamentally think of them as incredibly lightweight threads. They're the
- 6:24Speaker 2: smallest unit of work that can run concurrently, often just
- 6:27Speaker 2: a few kilobytes of memory stacked to start.
- 6:30Speaker 1: Super cheap compared to traditional OS threats.
- 6:33Speaker 2: Massively cheaper, and you launch one really easily. Just put
- 6:35Speaker 2: the go keyword before a function, call go my function.
- 6:38Speaker 1: That's it, and the function runs concurrently.
- 6:41Speaker 2: Yep. And interestingly, your main function the entry point of
- 6:45Speaker 2: your program is itself running as a gore routine.
- 6:48Speaker 1: Okay, so you can have potentially thousands of these running.
- 6:52Speaker 1: How do they talk to each other safely? That's where
- 6:54Speaker 1: channels come in exactly.
- 6:56Speaker 2: Channels are the preferred way for goroutines to communicate and synchronize.
- 7:01Speaker 2: They provide a typed conduit through which you can send
- 7:04Speaker 2: and receive values.
- 7:05Speaker 1: So instead of multiple groteins all trying to access and
- 7:08Speaker 1: modify the same variable directly.
- 7:10Speaker 2: Which leads to race conditions and complexity.
- 7:13Speaker 1: The past data between them through a channel.
- 7:15Speaker 2: Right, the go proverb is, don't communicate by sharing memory,
- 7:18Speaker 2: share memory by communicating. Channels facilitate that.
- 7:22Speaker 1: But there's a catch, isn't there? Since Maine is also
- 7:25Speaker 1: a guarantee.
- 7:25Speaker 2: Ah? Yes, the synchronization challenge. If your main function finishes and.
- 7:30Speaker 1: Exits, the whole program stops.
- 7:32Speaker 2: Correct. It doesn't matter if you have hundreds of other
- 7:35Speaker 2: gurroutines still doing important work in the background. If main returns,
- 7:39Speaker 2: they all just terminate.
- 7:41Speaker 1: So you need a way to make main weight.
- 7:42Speaker 2: You do, and the source material is very clear. Using
- 7:46Speaker 2: something like time sleep in Maine just to weight is
- 7:50Speaker 2: a bad hack. Don't do it.
- 7:52Speaker 1: That's the right way.
- 7:53Speaker 2: Proper synchronization tools. The sink package has things like weight group,
- 7:57Speaker 2: which lets main weight until a specific number of guroutines
- 8:00Speaker 2: have signaled they're done. Or for more complex scenarios, you'd
- 8:03Speaker 2: use the built in context package to manage the life cycle,
- 8:06Speaker 2: deadlines and cancelations of your grouteines properly. That's the robust
- 8:10Speaker 2: approach makes sense.
- 8:12Speaker 1: Let's shift gear slightly to IO input output. The notes
- 8:15Speaker 1: talk about ioreader and io writer. Why are these so important?
- 8:20Speaker 2: They're fundamental interfaces, hugely important. Ioreader defines the basic behavior
- 8:25Speaker 2: for reading a sequence of bytes. An io writer defines
- 8:28Speaker 2: writing a sequence of bytes.
- 8:29Speaker 1: Okay, simple enough, but why is that architecturally significant?
- 8:33Speaker 2: Because so many things in Go implement these interfaces, files,
- 8:37Speaker 2: network connections in memory, buffers, zip archives, encryption streams, ah.
- 8:42Speaker 1: So you can treat them all uniformly exactly.
- 8:44Speaker 2: It enables incredible composability. You can write a function that
- 8:48Speaker 2: takes any ioreader and it can read from a file
- 8:52Speaker 2: or a network socket or standard input without needing to change.
- 8:56Speaker 1: You could like chain them together, read from network, pass
- 8:59Speaker 1: through an encryptor write to.
- 9:00Speaker 2: A file precisely. If your encrypto takes a reader and
- 9:03Speaker 2: returns a reader, and your file writer takes a reader.
- 9:05Speaker 2: You just plug them together. Everything is treated as a
- 9:08Speaker 2: stream of bytes, massive flexibility from building systems.
- 9:12Speaker 1: And GO uses byte slices bite for this low level io.
- 9:16Speaker 2: Yes, because bytes are the universal currency of computing systems.
- 9:20Speaker 1: Right.
- 9:21Speaker 2: Using myte slices gives you very precise control over how
- 9:23Speaker 2: much data you're reading or writing.
- 9:25Speaker 1: Which is critical for network protocols or fixed format files.
- 9:28Speaker 2: Absolutely, you need that little level control.
- 9:30Speaker 1: Okay. And finally for this section, system utilities often need
- 9:33Speaker 1: command line arguments. How does GO handle those?
- 9:36Speaker 2: It's straightforward. They're available in a slice of strings called
- 9:40Speaker 2: dot rgs.
- 9:40Speaker 1: OS for the operating system package.
- 9:42Speaker 2: Right, and the key detail to remember is OS dot
- 9:45Speaker 2: org zero, the very first element in that slice.
- 9:47Speaker 1: What is it?
- 9:48Speaker 2: It's always the path to the executable file itself. The
- 9:51Speaker 2: actual arguments you type start from dot Org's one.
- 9:55Speaker 1: Good to know, so OS dot rs is never completely empty.
- 9:58Speaker 2: Correct, It always has at least that one element.
- 10:01Speaker 1: Okay, let's look at some more modern development patterns. Web
- 10:04Speaker 1: services often deal with Jason, sometimes messy Jason, where you
- 10:09Speaker 1: don't know the exact structure upfront. Ah.
- 10:11Speaker 2: Yes, the dreaded arbitrary Jason.
- 10:13Speaker 1: How does GO handle that gracefully? The notes mentioned map
- 10:17Speaker 1: string interface.
- 10:18Speaker 2: That's the go to structure. It's a map where the
- 10:20Speaker 2: keys are strings common for Jason objects, but the values
- 10:23Speaker 2: are interface.
- 10:25Speaker 1: The empty interface. What does that mean again?
- 10:27Speaker 2: It goes way of saying, this value could be anything
- 10:29Speaker 2: a string, a number, integer or float, a boolean, even
- 10:33Speaker 2: another nested map or slice.
- 10:35Speaker 1: So you can unmarshal almost any valid Jason into.
- 10:38Speaker 2: It pretty much. And the crucial benefit, which the source
- 10:41Speaker 2: points out, is that you don't lose type information immediately.
- 10:46Speaker 2: What do you mean, Well, if the Jason had a
- 10:48Speaker 2: number one twenty three, it stays a number type within
- 10:51Speaker 2: the interface value. If it had true, it stays a booleon.
- 10:55Speaker 2: You don't just convert everything to strings, which would lose
- 10:57Speaker 2: that original structure and type, and you.
- 10:59Speaker 1: Could figure out the actual type later using type assertions
- 11:02Speaker 1: or switches exactly.
- 11:03Speaker 2: It preserves the data fidelity, which is really important when
- 11:07Speaker 2: you're processing unknown structures.
- 11:09Speaker 1: Very useful for web APIs now Go evolved. For a
- 11:13Speaker 1: long time, interfaces and reflection were the main tools for
- 11:16Speaker 1: handling different types flexibly, but now we have generics.
- 11:19Speaker 2: Yes, generics landed properly and go one point one eight.
- 11:22Speaker 2: It was a big addition.
- 11:24Speaker 1: What's the core idea? How do they help?
- 11:26Speaker 2: Generics let you write functions and data structures that can
- 11:28Speaker 2: operate on a range of types you specify without you
- 11:31Speaker 2: having to write separate code for each type.
- 11:33Speaker 1: So less repetitive code, a lot less potentially.
- 11:37Speaker 2: The source material suggests that if you compare doing something
- 11:40Speaker 2: with interfaces versus doing it with generics, the generic version
- 11:43Speaker 2: is often simpler and shorter.
- 11:45Speaker 1: Code, especially for common tasks like working with slices or
- 11:49Speaker 1: maps of different custom types.
- 11:51Speaker 2: Right, think of writing a function that works on a
- 11:53Speaker 2: slice of integers, or a slice of floats or a
- 11:56Speaker 2: slice of your custom order type. Generics make that much
- 11:59Speaker 2: cleaner than faces and reflection typically would.
- 12:02Speaker 1: Cool and Go plays nicely with modern observability practices too.
- 12:06Speaker 2: Monitoring systems, Oh yeah, it's quite common. You can easily
- 12:09Speaker 2: build a small web server right into your GO application
- 12:12Speaker 2: using the standard FTTP package.
- 12:14Speaker 1: And use that to expose metrics exactly.
- 12:17Speaker 2: You can expose things like how many groutines are currently active,
- 12:20Speaker 2: how much memory is the process. Using custom application.
- 12:24Speaker 1: Counters and tools like Prometheus can scrape.
- 12:27Speaker 2: That data yep, Prometheus scraping is a very common pattern.
- 12:30Speaker 2: Then you can visualize it all in something like Rafana.
- 12:33Speaker 2: It integrates really well into that whole cloud native monitoring ecosystem.
- 12:37Speaker 1: Nice. Lastly, documentation Go has a specific way of doing it.
- 12:42Speaker 2: It's very simple, very effective. The rule highlighted in the
- 12:46Speaker 2: notes is strict a comment written immediately before a function, type, method,
- 12:51Speaker 2: or variable definition with no blank line in.
- 12:53Speaker 1: Between that comment. Because it's official documentation.
- 12:57Speaker 2: Correct the godoc tool picks it up automatically. Simple rule
- 13:01Speaker 2: makes documentation consistent.
- 13:02Speaker 1: And there's something about example code too.
- 13:04Speaker 2: Yeah, that's neat. You can write functions that start with
- 13:08Speaker 2: the word example, like example dotatoy. If you put a
- 13:11Speaker 2: special comment output at the end of that function, uh huh,
- 13:15Speaker 2: the Go test tool will actually run that example function
- 13:18Speaker 2: and check if it's printed output matches what's in your comment.
- 13:21Speaker 1: So your examples are also runnable.
- 13:22Speaker 2: Tests exactly, keeps the documentation accurate, and serves as a
- 13:26Speaker 2: basic test case. Very pragmatic hashtag tag outro.
- 13:30Speaker 1: Okay, so we've covered a lot of ground. The core
- 13:32Speaker 1: Go philosophy seems to be strict rules, clear.
- 13:36Speaker 2: Syntax, leading to predictability.
- 13:38Speaker 1: Powerful but well defined concurrency with gororitines and channels, milk
- 13:42Speaker 1: for scale, those really flexible io interfaces, reader and writer supercomposable,
- 13:47Speaker 1: and then modern features like handling arbitrary data with interface
- 13:50Speaker 1: and the addition.
- 13:50Speaker 2: Of generics, right covering the whole life cycle.
- 13:53Speaker 1: The recurring theme, as the source points out, is that
- 13:55Speaker 1: go is often called boring.
- 13:58Speaker 2: Which sounds negative, but in systems programming it's actually a compliment.
- 14:03Speaker 1: Right, predictable, reliable.
- 14:05Speaker 2: Understandable, exactly boring is good when you need systems that
- 14:09Speaker 2: don't fall over unexpectedly in the middle of the night.
- 14:12Speaker 2: That predictability is a massive virtue.
- 14:15Speaker 1: So what's the final takeaway for you? The listener writing code?
- 14:19Speaker 1: It seems to echo that classic advice.
- 14:21Speaker 2: Yeah, the material references wisdom often attributed to Joe Armstrong,
- 14:24Speaker 2: though it's a common sentiment, focus on correctness. First, write
- 14:28Speaker 2: code that is bug free.
- 14:30Speaker 1: May it work reliably, then make it fast enough.
- 14:33Speaker 2: Worrying about micro optimizing every last nanosecond too early is
- 14:36Speaker 2: often counterproductive. Premature optimization the root of all evil, as
- 14:41Speaker 2: Nuth famously put it, though maybe slightly misquoted. Sometimes right,
- 14:45Speaker 2: get it right, then make it fast if needed, Goh's
- 14:47Speaker 2: design helps you get it right.
- 14:49Speaker 1: Okay, so here's the final thought to leave you with
- 14:51Speaker 1: something to chew on. Go's simplicity was fundamental in enabling
- 14:56Speaker 1: huge complex systems like Kubernetes and Docker to even be
- 14:59Speaker 1: built manageably.
- 15:00Speaker 2: It teamed a certain level of complexity.
- 15:03Speaker 1: So, thinking ahead, what's the next layer of complexity in
- 15:07Speaker 1: software development that goes continued focus on discipline? Simplicity might
- 15:12Speaker 1: help us tackle what will it make boring and predictable next?
- 15:16Speaker 2: Hmmm, that's a really interesting question for the future.
- 15:19Speaker 1: Definitely something to ponder. That wraps up this deep dive.
- 15:22Speaker 1: Thanks for joining us
- 15:23Speaker 2: See you next time.