Latest / CyberSecurity Summary / Go Fundamentals: Gopher Guides
Transcript
- 0:00Speaker 1: Welcome to the deep dive. Today. We're really getting into
- 0:03Speaker 1: the fundamentals of the Go programming language. We're using Go
- 0:06Speaker 1: fundamentals Go for guides as our source here, and our
- 0:10Speaker 1: mission really is to pull out the core ideas how
- 0:14Speaker 1: GOT structures projects, it's basic elements, how it handles dependencies,
- 0:18Speaker 1: basically giving you a solid understanding without feeling like you're
- 0:21Speaker 1: reading a textbook exactly.
- 0:23Speaker 2: We'll be looking at modules, packages, dependencies obviously, but also
- 0:29Speaker 2: the language basics, numbers, strings.
- 0:32Speaker 1: Booleans, variables, constants.
- 0:34Speaker 2: Variables, constants, how to format things, and then collections like
- 0:37Speaker 2: arrays and slices, maps, control structures, functions, interfaces.
- 0:43Speaker 1: Error handling too, right, yeah, and concurrency it's a big one.
- 0:45Speaker 2: And go oh yeah, definitely, guroteines, channels, context synchronization, primitives,
- 0:50Speaker 2: even touching on the filesystem stuff. It's quite a bit,
- 0:53Speaker 2: but we'll break it down.
- 0:54Speaker 1: That's good. So for you listening, maybe you're prepping for
- 0:57Speaker 1: a tech chat, or just want to brush up on Go,
- 0:59Speaker 1: or you're just curious, this deep dive should give you
- 1:02Speaker 1: a structured, insightful look. Hopefully some moha moments along the way,
- 1:05Speaker 1: let's hope. So okay, let's start unpacking it at the
- 1:08Speaker 1: very foundation. How does Go organized code.
- 1:11Speaker 2: Right, It really starts with modules. Think of the Go
- 1:15Speaker 2: dot mod file. It's goes way of promising reproducible builds.
- 1:19Speaker 1: Reproducible builds meaning naing.
- 1:21Speaker 2: Everyone on the team or you know, your build server
- 1:23Speaker 2: uses the exact same versions of all the libraries. The
- 1:26Speaker 2: Go dot mod file names your project, says what Go
- 1:29Speaker 2: version it needs. Like the examples we looked at use
- 1:31Speaker 2: Go one point nineteen.
- 1:32Speaker 1: Okay, so godat mod is the project manifest and inside
- 1:35Speaker 1: modules we have packages exactly.
- 1:37Speaker 2: Packages are the fundamental unit of importable code. The key
- 1:41Speaker 2: thing with naming them. Keep it short, simple, easy to recognize.
- 1:44Speaker 1: Like squile L or HDPP precisely.
- 1:47Speaker 2: Much better than say structured query language. Imagine typing that
- 1:50Speaker 2: out every time you use it.
- 1:51Speaker 1: Yeah, that would get old fast. Yeah. What if you
- 1:54Speaker 1: have two packages with the same name, like from different places.
- 1:57Speaker 2: Ah, good question. That's where import aliasing comes in. If
- 2:00Speaker 2: you import say demo fubar and maybe another place bar,
- 2:03Speaker 2: you can give one an alias like import pub demo fubar.
- 2:06Speaker 1: Oh I see, so you'd use pub dot function name
- 2:08Speaker 1: for one and maybe just bar another function for the
- 2:11Speaker 1: other exactly.
- 2:12Speaker 2: It avoids those name clashes and keeps things clear.
- 2:14Speaker 1: Okay, so go dot mod tracks external stuff packages organize
- 2:18Speaker 1: your internal code. How do you actually get those external libraries?
- 2:21Speaker 2: That's the go get command. So if you need let's
- 2:24Speaker 2: say that fleck library for working with words, Yeah, you
- 2:27Speaker 2: just run Go get GitHub dot Com forward Slash, go Buffalo, flecked,
- 2:31Speaker 2: Go fetches. It updates your Go dot mod with the dependency.
- 2:35Speaker 1: In the version, and it also creates that go dot
- 2:37Speaker 1: sum file. Right. What's that for again?
- 2:40Speaker 2: Right, the go dot sum It's basically a lockfile with
- 2:43Speaker 2: cryptographic hashes like digital fingerprints for your dependencies. It ensures
- 2:46Speaker 2: the code you downloaded is exactly what it's supposed to be,
- 2:49Speaker 2: hasn't been tampered with. Consistency and security, got.
- 2:53Speaker 1: It like a security checkpoint and versions. How does go
- 2:56Speaker 1: handle updates?
- 2:57Speaker 2: It uses semantic versioning, you know, the major dot minor
- 3:01Speaker 2: dot patch system.
- 3:02Speaker 1: Where major means breaking changes, minor means new features, Patch
- 3:05Speaker 1: means fixes pretty much.
- 3:07Speaker 2: Yeah, Go get usually updates you to the latest minor
- 3:10Speaker 2: or patch versions safely. One thing I'll watch out for, though,
- 3:13Speaker 2: is sychnical imports.
- 3:15Speaker 1: Where package A needs b and B needs.
- 3:17Speaker 2: A Yeah, that can get messy fast, generally best to
- 3:20Speaker 2: avoid that.
- 3:21Speaker 1: Structure makes sense, Okay, modules, packages, dependencies clear. Now let's
- 3:25Speaker 1: dive into the language itself, the basics.
- 3:27Speaker 2: Okay, So GO is statically typed.
- 3:31Speaker 1: Meaning it checks types before you run the code.
- 3:34Speaker 2: Exactly, catches lots of errors early, like trying to add
- 3:37Speaker 2: text to a number. It's also compiled straight to machine code,
- 3:40Speaker 2: so it's fast, and it has garbage collection.
- 3:42Speaker 1: The automatic memory cleanup.
- 3:44Speaker 2: Crew that's the one takes a lot of manual memory
- 3:47Speaker 2: management burden off the developer.
- 3:49Speaker 1: Big plus definitely sounds helpful. Yeah, okay, let's talk numbers.
- 3:52Speaker 1: What are the options?
- 3:53Speaker 2: GO gives you a fair range. You've got integers signed
- 3:56Speaker 2: in tate through in sixty four and unsigned and dad
- 3:58Speaker 2: on sixty four plus t us the general int and
- 4:01Speaker 2: that depend on your system architecture, and for decimals float
- 4:04Speaker 2: thirty two and float sixty four. Usually float sixty four
- 4:07Speaker 2: is the default choice unless you have specific needs.
- 4:10Speaker 1: That's a lot of integer types. How do you choose?
- 4:12Speaker 2: Often you can just start with int for whole numbers
- 4:16Speaker 2: and float sixty four for decimals. The specific sizes like
- 4:19Speaker 2: in eight become important when you're really optimizing for memory
- 4:22Speaker 2: or you know, dealing with specific hardware interfaces.
- 4:25Speaker 1: Okay, what about putting two big a number into a
- 4:27Speaker 1: small type overflow right overflow?
- 4:31Speaker 2: The compiler might catch it if you try to assign
- 4:33Speaker 2: a value that's obviously too big, like varx once equals
- 4:37Speaker 2: three hundred, that won't compile.
- 4:39Speaker 1: But what about during calculations.
- 4:40Speaker 2: Ah, that's different. If you add say one to a
- 4:43Speaker 2: one eight that's already two fifty five, it doesn't crash.
- 4:45Speaker 1: It wraps around, wraps around two zero yep.
- 4:48Speaker 2: Wraps around to zero. Go doesn't automatically saturate or stop
- 4:51Speaker 2: the calculation by default.
- 4:53Speaker 1: Something to be aware of, good to know, subtle. Okay,
- 4:56Speaker 1: what about text strings In GO?
- 4:59Speaker 2: Strings are pretty straight forward. You've got your standard double
- 5:01Speaker 2: quoted strings. They interpret escape sequences like in for new.
- 5:04Speaker 1: Line t for tabs, standard stuff.
- 5:06Speaker 2: And then you have raw string literals using backticks inside
- 5:09Speaker 2: these backslashes or just backslashes, no special meaning.
- 5:13Speaker 1: That sounds useful.
- 5:14Speaker 2: It's great for multiline strings, like embedding JSON or writing
- 5:17Speaker 2: regular expressions where you'd otherwise be escaping constantly. And GO
- 5:22Speaker 2: handles UTF eight out of the box, so complex characters
- 5:25Speaker 2: different languages.
- 5:25Speaker 1: Usually just works and what's a roun?
- 5:28Speaker 2: A roune is basically an alias for N thirty two.
- 5:30Speaker 2: It represents a single Unicode code point, which might be
- 5:33Speaker 2: one or more bites in UTF E.
- 5:35Speaker 1: Okay, so strings cover text? Well? Yeah, what about simple
- 5:38Speaker 1: true or false?
- 5:39Speaker 2: That's the bool type values are just true or false
- 5:43Speaker 2: essential for conditions obviously.
- 5:45Speaker 1: Right, numbers, text booleans. Yeah, how do we actually hold
- 5:48Speaker 1: onto these values? Variable?
- 5:49Speaker 2: Yep, Variables You declare them with var than the name
- 5:52Speaker 2: than the type like var count int.
- 5:54Speaker 1: You assign with what If you declare but don't a sign,
- 5:57Speaker 1: then it.
- 5:57Speaker 2: Gets the zero value for its type zero for numbers,
- 6:00Speaker 2: empty strength for strings, false for bulls, and nil for pointers, slices, maps, interfaces,
- 6:04Speaker 2: et cetera.
- 6:05Speaker 1: Mil Okay, is there a shorter way to declare? Yes?
- 6:08Speaker 2: The short variable declaration name value. This declares and initializes
- 6:13Speaker 2: in one go and go infers the type from the value.
- 6:16Speaker 1: Very common, ah, the amperator got it?
- 6:18Speaker 2: And naming rules start with a letter or underscore can
- 6:22Speaker 2: contain letters, numbers, underscores, case sensitive, can't be a keyword
- 6:27Speaker 2: like funk or if and go style prefers short names
- 6:31Speaker 2: often yes, especially for variables with a very limited scope
- 6:34Speaker 2: like loop counters Terseness is common, but clarity still matters,
- 6:38Speaker 2: of course makes sense.
- 6:39Speaker 1: And constants, how are they different?
- 6:41Speaker 2: Declared with const The key thing is their value cannot
- 6:44Speaker 2: be changed after declaration. They're fixed. They can be typed
- 6:48Speaker 2: or untyped. Untyped constants are a bit more flexible.
- 6:50Speaker 1: Okay, var for changeable const for fixed. Now, how do
- 6:54Speaker 1: we display these values? Formatting?
- 6:57Speaker 2: That's where the FMT package comes in. It's your standard
- 6:59Speaker 2: in output.
- 7:00Speaker 1: Toolkit FMT dot pron FMT dot print of those ones.
- 7:03Speaker 2: Exactly, printlin prints things with spaces in between, and adds
- 7:06Speaker 2: a newline. Print for formatted output using.
- 7:09Speaker 1: Verbs verbs like percent or percent right.
- 7:12Speaker 2: Percent is the default value. Format prients two shows the
- 7:14Speaker 2: type percent for decimal integers, percents for strings, percent reported strings,
- 7:18Speaker 2: percent for floats, lots of options sprintoff works like print,
- 7:21Speaker 2: but returns the formatted string instead of printing.
- 7:23Speaker 1: It useful and you can control padding and stuff.
- 7:26Speaker 2: Yep padding integers, printing signs showing base prefixes for binary pers,
- 7:31Speaker 2: octal perso x, percentx escape sequences like N and T
- 7:35Speaker 2: work in strings too. FMT is pretty powerful for basic formatting.
- 7:40Speaker 1: Cool. Okay, basic types and printing cover. Let's move on
- 7:43Speaker 1: to collections, arrays, and slices.
- 7:45Speaker 2: Right, So, rais and GO have a fixed size. You
- 7:48Speaker 2: declare an array, say varnums five int, and it always
- 7:51Speaker 2: has five integers. Can't change that size later.
- 7:54Speaker 1: And all elements must be the same time correct.
- 7:55Speaker 2: If you don't initialize it, elements get the zero value.
- 7:59Speaker 2: The compiler also helps prevent you from accessing an index
- 8:01Speaker 2: that's out of bounds. Importantly, arrays are value types value.
- 8:05Speaker 1: Types, meaning if you assign one array to another, it
- 8:07Speaker 1: copies everything exactly.
- 8:09Speaker 2: You get a full independent copy.
- 8:10Speaker 1: Okay, so fixed size value types. That sounds limiting sometimes,
- 8:14Speaker 1: what about slices.
- 8:15Speaker 2: That's where slices come in. They are dynamically sized flexible
- 8:18Speaker 2: views into an underlying array, much more common in GO
- 8:21Speaker 2: code than raw arrays. The view into an array, yeah,
- 8:25Speaker 2: a slice has a pointer to the underlying array, a
- 8:28Speaker 2: length how many elements it currently holds, and a capacity
- 8:32Speaker 2: how large the underlying array segment is. You can create
- 8:35Speaker 2: them with it literal like in one, two, three, or
- 8:38Speaker 2: using the make function. Make yent five creates a slice
- 8:42Speaker 2: of five integers initialized to zero. Make zero ten creates
- 8:47Speaker 2: an empty slice, but with an underlying array capacity of
- 8:50Speaker 2: ten ready to.
- 8:50Speaker 1: Grow, and slices are reference types unlike arrays.
- 8:54Speaker 2: Correct assigning a slice just copies the slice header pointer, length, capacity,
- 8:58Speaker 2: not the underlying beta. Both slices will point to the
- 9:01Speaker 2: same data.
- 9:01Speaker 1: Ah, so changes through one slice are visible through the other.
- 9:04Speaker 2: If they point to the same underlying elements.
- 9:06Speaker 1: Yes, how do you add elements to a slice? It's
- 9:08Speaker 1: it's their dynamic.
- 9:09Speaker 2: You use the built in a pen function. My slice,
- 9:11Speaker 2: a pend, my slice new value. If there's enough capacity
- 9:14Speaker 2: in the underlying array, it just adds the element and
- 9:16Speaker 2: increases the length.
- 9:17Speaker 1: And if there isn't enough capacity.
- 9:19Speaker 2: Then a pen allocates a new, larger underlying array, copies
- 9:23Speaker 2: the old data over adds the new element, and returns
- 9:26Speaker 2: a slice pointing to this new array. That's why you
- 9:28Speaker 2: always assign the result of a pend back to your
- 9:30Speaker 2: slice variable.
- 9:31Speaker 1: Got it? You can depend a whole other slice too, right, yep?
- 9:33Speaker 2: Using the operator a pend slice one, slice two. And
- 9:37Speaker 2: there's a copy function to explicitly copy elements between slices.
- 9:40Speaker 1: Okay, fixed arrays flexible slices, how do we loop through them?
- 9:44Speaker 2: The four loop is goes only looping construct you can
- 9:46Speaker 2: use the classic C style for with an index for
- 9:50Speaker 2: i or o omeaning my slice i plus plus slice
- 9:53Speaker 2: familiar but more idiomatic, and go is using with a
- 9:57Speaker 2: range keyword for index value range my flesh. It iterates
- 10:01Speaker 2: over the collection, giving you the index and the value
- 10:03Speaker 2: at each step.
- 10:04Speaker 1: Range works on a rais and slices what else.
- 10:06Speaker 2: Maps strings gives indexed and run and channels only built
- 10:10Speaker 2: in collection types. Mostly inside loops, you can use continue
- 10:14Speaker 2: to skip to the next iteration or break to exit
- 10:16Speaker 2: the loop entirely.
- 10:17Speaker 1: Standard loop control. Okay, foreign range. Let's talk about maps
- 10:21Speaker 1: key value pairs.
- 10:22Speaker 2: Exactly like dictionaries or hash maps and other languages rate
- 10:26Speaker 2: for lookups, you declare them using make map key type,
- 10:29Speaker 2: value type, or a map literal like map string at one, one,
- 10:32Speaker 2: two to two.
- 10:33Speaker 1: How do you add or get values.
- 10:34Speaker 2: Using square brackets with the key my map key value
- 10:38Speaker 2: to set and vowel my map key to.
- 10:40Speaker 1: Get What if the key isn't there when you try
- 10:41Speaker 1: to get it.
- 10:42Speaker 2: You get the zero value for the value type e g.
- 10:44Speaker 2: Zero for end for string. But there's a better way
- 10:47Speaker 2: to check if it exists. The calm op idiom value okay,
- 10:52Speaker 2: my map key here okay will be boolean true if
- 10:55Speaker 2: the key existed false. Otherwise value holds the actual value
- 10:59Speaker 2: if okay is true, or the zero value if false.
- 11:02Speaker 2: Prevents confusion between a stored zero value and a missing key.
- 11:05Speaker 1: Ah, that's neat. Do maps have a fixed size?
- 11:08Speaker 2: No, they grow dynamically as you add more entries. Practically
- 11:11Speaker 2: unlimited really just limited by memory. Like slices.
- 11:13Speaker 1: There are reference types, and the order you get things
- 11:15Speaker 1: back when iterating isn't guaranteed correct.
- 11:17Speaker 2: Iteration order over a map is intentionally randomized and go.
- 11:21Speaker 2: Don't rely on it got.
- 11:22Speaker 1: It maps for lookups, no guaranteed order. How about making
- 11:26Speaker 1: decisions if statements.
- 11:27Speaker 2: Yip standard to if lcfls. One neat GO feature is
- 11:31Speaker 2: that you can declare a variable scope just to the
- 11:33Speaker 2: if statement itself. If value okay, my map key okay,
- 11:38Speaker 2: value and okay only exist inside the if in any
- 11:41Speaker 2: else blocks.
- 11:41Speaker 1: Oh that's tidy, keeps the scope tight. Yeah. What about switch?
- 11:44Speaker 2: Switch is powerful and go. You can switch on a
- 11:46Speaker 2: value and then have different case blocks. Unlike see your Java,
- 11:49Speaker 2: there's no automatic fall through. A matched case executes and
- 11:52Speaker 2: then the switch is done.
- 11:53Speaker 1: No need for break everywhere exactly.
- 11:56Speaker 2: If you want fall through, you have to use the
- 11:57Speaker 2: fall through keyword explicitly. You can lists multiple values in
- 12:01Speaker 2: a single case, and there's a default case. You can
- 12:03Speaker 2: even use switch without an expression, making each case just
- 12:06Speaker 2: a boolean condition switch case by zero, case by.
- 12:09Speaker 1: Zero like a cleaner if lsif chain nice. Let's package
- 12:13Speaker 1: up reusable code functions.
- 12:15Speaker 2: Right define with the funk keyword. Function function name r
- 12:18Speaker 2: one type one rchqwo type two return type. The main
- 12:21Speaker 2: function in the main package is the entry point for
- 12:24Speaker 2: executable programs.
- 12:25Speaker 1: You have to declare the type for every argument.
- 12:27Speaker 2: Yes always funk ad a in b nt in return
- 12:32Speaker 2: a plus b. You can group types if consecutive ARBs
- 12:36Speaker 2: have the same type funk ad a b n t in,
- 12:40Speaker 2: and return values can return zero or more values. Just
- 12:43Speaker 2: list the return types after the arguments. Funk divide ab
- 12:46Speaker 2: float sixty four, float sixty four error. Returning an error
- 12:50Speaker 2: as the last value is the standard go away to
- 12:52Speaker 2: signal failure.
- 12:53Speaker 1: Multiple return values are common, then.
- 12:55Speaker 2: Very common, especially for the value and error pattern. You
- 12:58Speaker 2: can also name the return values funk split some int
- 13:02Speaker 2: x ynt. They act like variables declared at the start
- 13:04Speaker 2: of the function. If a function returns values, the caller
- 13:07Speaker 2: has to handle or explicitly ignore them using funk.
- 13:10Speaker 1: Okay, what if a function needs to accept, say any
- 13:14Speaker 1: number of arguments.
- 13:15Speaker 2: That's very atic. Arguments use before the type of the
- 13:18Speaker 2: last parameter funks, numbs, dot nt intinct. Inside the function,
- 13:22Speaker 2: numbs is treated as a slice event, so you could.
- 13:24Speaker 1: Call some one to two or some one two.
- 13:26Speaker 2: Three four exactly. Very flexible for things like logging functions
- 13:30Speaker 2: or database query builders.
- 13:32Speaker 1: Cool. Now, what about defer That seems unique to.
- 13:34Speaker 2: Go It is pretty distinctive. Differ schedules a function call
- 13:39Speaker 2: to be executed just before the surrounding function.
- 13:43Speaker 1: Returns, before it returns like at.
- 13:44Speaker 2: The very end yep whether the function returns normally or panics.
- 13:48Speaker 2: The deferred calls run. If you have multiple defers, they
- 13:52Speaker 2: execute in last in first out laf order.
- 13:56Speaker 1: I have a FILT, so the last one deferred runs first.
- 13:59Speaker 2: Correct. The most common use is resource cleanup, like open
- 14:02Speaker 2: a file, then immediately defer file close. Guarantees it gets closed.
- 14:06Speaker 1: Even if there's an error later in the function.
- 14:08Speaker 2: Exactly. It simplifies cleanup logic immensely. One key point the
- 14:13Speaker 2: arguments to the deferred function are evaluated when the defer
- 14:16Speaker 2: statement is executed, not when the call actually happens.
- 14:19Speaker 1: Ah. Okay, So if you defer function using a variable,
- 14:22Speaker 1: it uses the variable's value at that moment.
- 14:24Speaker 2: Right, Something to keep in mind, especially with loops and
- 14:26Speaker 2: anonymous functions.
- 14:28Speaker 1: Got it defer for guaranteed cleanup? What about the innit function?
- 14:31Speaker 2: A knit is special. It runs automatically when its package
- 14:34Speaker 2: is initialized before main runs. You don't call it yourself?
- 14:37Speaker 1: When would you use it?
- 14:38Speaker 2: Usually for package levels setup you can have multiple init
- 14:41Speaker 2: functions in a file or package. Within a file, they
- 14:44Speaker 2: run in order of appearance across files in a package.
- 14:47Speaker 1: The order isn't guaranteed, so maybe setting up database connections
- 14:50Speaker 1: or registering.
- 14:51Speaker 2: Things sometimes use for that. Yeah, like registering image formats
- 14:54Speaker 2: by importing the image package for its in it side effect.
- 14:57Speaker 2: But relying on import side effects is often seen as
- 15:00Speaker 2: an anti pattern. Now, better to be explicit.
- 15:03Speaker 1: Okay, in it for setup, but use with caution. Let's
- 15:06Speaker 1: move to structuring more complex data structs and methods.
- 15:09Speaker 2: Structs let you group different pieces of data together into
- 15:12Speaker 2: a single custom type type, person, struct, name, string, age.
- 15:17Speaker 1: And p. Simple enough, how do you access the fields?
- 15:20Speaker 2: Dot notation dot p person name, Alice age thirty f
- 15:24Speaker 2: mt printline P name.
- 15:25Speaker 1: What about embedding.
- 15:27Speaker 2: AH, Embedding is like anonymous fields type employee struct salary
- 15:31Speaker 2: float sixty four. Now an employee value E has field
- 15:35Speaker 2: Z salary, but also E name and ehe promoted from
- 15:38Speaker 2: the embedded person struct.
- 15:39Speaker 1: Do you access the embedded fields directly? Cool and methods
- 15:43Speaker 1: functions attached to types.
- 15:44Speaker 2: Exactly, You define a method with a receiver funker pee
- 15:48Speaker 2: person Hell, my name is peak person p person grant
- 15:52Speaker 2: here pee person is the receiver.
- 15:53Speaker 1: You can have pointer receivers too, right, yeah, funk person
- 15:56Speaker 1: setage new agent. Right.
- 15:57Speaker 2: The big difference pointer receivers person can modify the original struct.
- 16:02Speaker 2: Value value receivers person operate on a copy of the struct,
- 16:07Speaker 2: so modifications inside the method don't affect the original.
- 16:11Speaker 1: So use pointers if you need to change the struct.
- 16:13Speaker 2: Generally yes, or if the struct is very large and
- 16:16Speaker 2: you want to avoid copying it for performance reasons. Value
- 16:19Speaker 2: receivers are simpler if you don't need mutation.
- 16:22Speaker 1: Can you add methods to any type like built.
- 16:24Speaker 2: In types, not directly to built ins like INNT, but
- 16:27Speaker 2: you can define a new type based on it. Type
- 16:30Speaker 2: my int int and add methods to my ind However,
- 16:33Speaker 2: the new type doesn't inherit methods from the underlying type.
- 16:36Speaker 2: You can also define methods for types from other packages,
- 16:38Speaker 2: but only within the package where you define the method.
- 16:41Speaker 1: What about calling methods on nil pointers?
- 16:43Speaker 2: That can cause a panic if you're not careful. Methods
- 16:47Speaker 2: with pointer receivers should ideally check if the receiver is nil.
- 16:51Speaker 2: If p equals nil.
- 16:53Speaker 1: Good practice, Okay, structs and methods. What about pointers themselves?
- 16:58Speaker 2: Pointers hold the memory address of a value. GO is
- 17:01Speaker 2: pass by value, meaning function arguments are copies. Pointers let
- 17:05Speaker 2: you effectively pass by a reference, allowing functions to modify
- 17:09Speaker 2: the original value.
- 17:10Speaker 1: How do you work with them?
- 17:11Speaker 2: Type declares a pointer type for example int, and the
- 17:15Speaker 2: variable gets the memory address. Creates a pointer pointer variable
- 17:18Speaker 2: to references the pointer to get the value it points to.
- 17:21Speaker 1: So she takes the address follows the address pretty much.
- 17:24Speaker 2: Using pointers avoids copying large data structures, which can be
- 17:26Speaker 2: good for performance, but it also means you need to
- 17:28Speaker 2: think about shared access and potential garbage collector impact.
- 17:31Speaker 1: Got it pointers for indirect access and modification? Now testing
- 17:35Speaker 1: crucial part of development. How does go do it?
- 17:37Speaker 2: Go has great built in testing test files, end and
- 17:40Speaker 2: test dot go test functions start with test and take
- 17:43Speaker 2: a testing dot T argument test my function testing dot T.
- 17:46Speaker 1: And it is used for reporting.
- 17:48Speaker 2: Failures right TFA TALF expected percingop percy and expected actual
- 17:53Speaker 2: logs and error, but continues t fetalf logs and stops
- 17:56Speaker 2: the current test immediately. Good error messages are key.
- 17:58Speaker 1: How do you run test Go tests in.
- 18:00Speaker 2: The directory go test dash v gives verbose output showing
- 18:04Speaker 2: each test run.
- 18:05Speaker 1: What about code coverage? Seeing what percentage of your code
- 18:08Speaker 1: the tests hit.
- 18:09Speaker 2: Easy, Go test dash Tierra profile coverage dot out generates
- 18:13Speaker 2: a profile. Then Go tool cover dash HTML coverage dot
- 18:17Speaker 2: out opens a nice HTML report showing covered and uncovered lines.
- 18:21Speaker 2: Very useful.
- 18:21Speaker 1: That sounds great for finding gaps. Any techniques for organizing tests, Table.
- 18:25Speaker 2: Driven tests are very popular and effective. You define a
- 18:28Speaker 2: slice of test cases often strucks, each containing inputs and
- 18:31Speaker 2: expected outputs. Then one loop runs all the cases.
- 18:34Speaker 1: Keeps things dry, don't repeat yourself exactly and clear.
- 18:38Speaker 2: Go also supports subtests using t run subtest name funct
- 18:41Speaker 2: testing dot T you. This gives better output organization and
- 18:44Speaker 2: lets you run specific subtests. You can even run subtests
- 18:47Speaker 2: in parallel with t.
- 18:48Speaker 1: Parallel tests nice any other useful GO test flags lots.
- 18:52Speaker 2: Short skips, long running tests if you mark them. Booker
- 18:56Speaker 2: na sets the number of tests to run in parallel.
- 18:58Speaker 2: You can run specific tests by name using NUSH run rejects,
- 19:01Speaker 2: nushtime out duration, NESHO, foul fast, NUSH account one to
- 19:04Speaker 2: disable caching, plenty.
- 19:06Speaker 1: Of control, and test helper functions.
- 19:08Speaker 2: Yeah common practice to extract reusable logic into helper functions
- 19:12Speaker 2: within the test file. Conventionally, they take testing dot t
- 19:15Speaker 2: B as the first argument, which works for both tests
- 19:18Speaker 2: testing dot t and benchmarks testing B.
- 19:20Speaker 1: Cool solid testing framework. Let's switch gears to interfaces, a
- 19:24Speaker 1: core concepting GO.
- 19:26Speaker 2: Interfaces define a set of method signatures. They specify behavior.
- 19:30Speaker 2: What makes Go different is implicit implementation.
- 19:33Speaker 1: Implicit meaning you don't have to say my type implements
- 19:36Speaker 1: my interface exactly.
- 19:37Speaker 2: If your type has all the methods declared in the
- 19:39Speaker 2: interface with the right signatures, it automatically implements that interface,
- 19:43Speaker 2: no explicit declaration needed.
- 19:45Speaker 1: Interesting like the io writer example.
- 19:47Speaker 2: Perfect example, io writer requires a right byte int error method.
- 19:54Speaker 2: Both o's file and bytes buffer have that method, so
- 19:57Speaker 2: they both satisfy io writer even though they're very different types. Internally,
- 20:02Speaker 2: you can create your own types that implement interfaces to
- 20:05Speaker 2: So it's.
- 20:05Speaker 1: If it walks like a duck and quacts like.
- 20:07Speaker 2: A duck, it implements the duck interface.
- 20:09Speaker 1: Basically, Yeah, what are the benefits?
- 20:11Speaker 2: Huge flexibility, abstraction. You can write functions that accept an
- 20:15Speaker 2: interface type and they'll work with any concrete type that
- 20:18Speaker 2: provides the required behavior. Great for decoupling parts of your
- 20:21Speaker 2: system and for testing. You can easily pass in mock implementations.
- 20:25Speaker 1: And the advice is to keep interfaces small.
- 20:28Speaker 2: Yes, very much so. The GO philosophy favors small, focused interfaces,
- 20:33Speaker 2: often with just one or two methods. Interfaces define only methods,
- 20:37Speaker 2: not fields.
- 20:37Speaker 1: Can you use interfaces to make functions more specific about
- 20:40Speaker 1: what they need?
- 20:41Speaker 2: Definitely? Instead of insert in data any, maybe insert in model.
- 20:46Speaker 2: Where a model is an interface requiring an id int method.
- 20:49Speaker 2: It clarifies the expectation.
- 20:51Speaker 1: What about the empty interface?
- 20:52Speaker 2: Any any or interface is the interface with zero methods.
- 20:56Speaker 2: Since every type has zero or more methods, every type
- 21:00Speaker 2: implements any so.
- 21:01Speaker 1: It can hold literally anything.
- 21:03Speaker 2: Yes, but you lose static type safety. To get the
- 21:06Speaker 2: underlying concrete value back, you need a type assertion.
- 21:09Speaker 1: The comic coaduck again vow okay, buy any dot string exactly.
- 21:14Speaker 2: That's the safe way. If the assertion fails, okay as
- 21:17Speaker 2: falls no panic. You can also use a type switch
- 21:21Speaker 2: switch v ANI types to handle multiple possible types safely okay.
- 21:25Speaker 1: Any for ultimate flexibility, but use assertions carefully. Now, errors
- 21:29Speaker 1: and panics. How does go handle things going wrong?
- 21:33Speaker 2: The standard way is using the built in air interface.
- 21:36Speaker 2: It just requires an error string method. Functions that can
- 21:38Speaker 2: fail return an error as their last return value. If
- 21:41Speaker 2: everything's okay, they return nil.
- 21:42Speaker 1: For the error, So check if air nil.
- 21:45Speaker 2: That's the standard pattern. Yes, you can create your own
- 21:47Speaker 2: custom err types too, just by defining a type with
- 21:49Speaker 2: an error method.
- 21:50Speaker 1: What about panics? When do they happen?
- 21:52Speaker 2: Panics are for truly unexpected, unrecoverable situations at run time,
- 21:56Speaker 2: things like index out of bounds, nil point, or to
- 21:59Speaker 2: reference stuff that likely indicates a programming bug. You can
- 22:03Speaker 2: trigger one explicitly with panic something went very wrong. Panics
- 22:06Speaker 2: unwind the call stack.
- 22:08Speaker 1: Can you stop a panic from crashing the whole program?
- 22:11Speaker 2: Yes, using recover, but it only works if called directly
- 22:15Speaker 2: inside a deferred function.
- 22:16Speaker 1: Only inside a defer yep.
- 22:18Speaker 2: If a panic occurs, deferred functions run. If one calls recover,
- 22:22Speaker 2: the panic stops and recover returns the value past panic.
- 22:26Speaker 2: Useful for preventing a single guarantine failure from taking down
- 22:29Speaker 2: the server.
- 22:30Speaker 1: For example, what are common causes of panics?
- 22:32Speaker 2: Nil pointer to references are a big one. Trying to
- 22:34Speaker 2: access a field on an uninitialized struct pointer or calling
- 22:38Speaker 2: a method on a nil interface value. Also calling a
- 22:41Speaker 2: function variable that's nil.
- 22:43Speaker 1: Right, got to initialize things? What about adding more context
- 22:46Speaker 1: to errors wrapping?
- 22:47Speaker 2: Error wrapping is great for that. Using fmt dot air
- 22:50Speaker 2: with the percent of a verb lets you wrap an
- 22:52Speaker 2: existing air inside a new one, adding context fmt dot
- 22:56Speaker 2: air opening configure dot percent of view air so.
- 22:59Speaker 1: You can see the chain of error exactly.
- 23:01Speaker 2: Errors dot unrapper gets you the underlying error. You can
- 23:05Speaker 2: implement unwrap methods on custom error types too. This helps
- 23:09Speaker 2: track the root.
- 23:10Speaker 1: Cause and errors is an errors.
- 23:12Speaker 2: Errors is Their target checks if any error in the
- 23:16Speaker 2: chain matches the target error, either a specific sentinel value
- 23:19Speaker 2: or type errors as are and targetvar tries to find
- 23:23Speaker 2: an error of a specific type in the chain and
- 23:26Speaker 2: assigns it to targetbar. Very useful for handling specific error
- 23:29Speaker 2: types programmatically.
- 23:31Speaker 1: Okay, air handling patterns are clear. What about stack traces?
- 23:34Speaker 1: Seeing the call sequence.
- 23:36Speaker 2: The runtime BBUG package helps there, debug stack returns the
- 23:40Speaker 2: current stack trace, debug dot print stack prints. It invaluable
- 23:44Speaker 2: when debugging tricky errors or panics.
- 23:46Speaker 1: Definitely okay. Let's move on to a newer feature, generics.
- 23:49Speaker 1: What problem do they solve?
- 23:50Speaker 2: Before generics, writing functions or types that worked with multiple
- 23:54Speaker 2: unrelated types often involved using interface any and lots of
- 23:57Speaker 2: type assertions, which was cumbersome and less type safe.
- 24:00Speaker 1: Like writing a keys function for a map that could
- 24:03Speaker 1: work for maps with string keys or in keys.
- 24:05Speaker 2: Exactly generics, let you write that function once using type parameters.
- 24:09Speaker 2: You declare them in square brackets funk KEYSK comparable v
- 24:14Speaker 2: any any m map kV k k.
- 24:17Speaker 1: Comparable v any. Those are type parameters.
- 24:19Speaker 2: With constraints right, K and V are the type parameters.
- 24:22Speaker 2: Comparable is a constraint, meaning K must be a type
- 24:25Speaker 2: that can be compared, like map keys need to be
- 24:27Speaker 2: any means V can be any. Type. Constraints limit what
- 24:30Speaker 2: types can be used.
- 24:31Speaker 1: How does the compiler know what K and V are
- 24:33Speaker 1: when you call the function.
- 24:35Speaker 2: Often through type inference based on the arguments. If you
- 24:38Speaker 2: pass a map string in, it figures out P is
- 24:40Speaker 2: string and V isnt. Sometimes you might need to specify
- 24:43Speaker 2: them explicitly key string in my map.
- 24:45Speaker 1: Can you have multiple parameters and complex constraints?
- 24:48Speaker 2: Yep, multiple type parameters, constraints defined using interfaces, specifying required methods,
- 24:53Speaker 2: combining constraints with n using to allow underlying types type
- 24:58Speaker 2: my end int could satisfy INT. The constraints package offers
- 25:01Speaker 2: predefined ones like ordered integer, float, and.
- 25:04Speaker 1: You can define generic types too, like strucks, dot type,
- 25:07Speaker 1: no t any structure Absolutely.
- 25:09Speaker 2: Generic types are very useful even with generics. Sometimes you
- 25:12Speaker 2: might still need type assertions inside the generic function if
- 25:15Speaker 2: you need to access type specific behavior not covered by
- 25:18Speaker 2: the constraints.
- 25:19Speaker 1: Generics sound powerful for reusable, type safe code. Now concurrency
- 25:26Speaker 1: Goh's famous for this.
- 25:27Speaker 2: It is important distinction. First. Concurrency is about managing multiple tasks,
- 25:31Speaker 2: maybe interleaving them. Parallelism is about executing multiple tasks simultaneously.
- 25:37Speaker 2: Go makes concurrency easy. Their routines right guroutines are lightweight
- 25:41Speaker 2: concurrent functions. You launch one just by putting go before
- 25:44Speaker 2: function call to go. My funk very cheap to create
- 25:47Speaker 2: thousands of them.
- 25:48Speaker 1: And the go run times schedules them onto OS threads.
- 25:50Speaker 2: Yeah, the ghost scheduler manages multiplexing groutines onto a smaller
- 25:54Speaker 2: pool of OS threads, handling things like work sharing and
- 25:57Speaker 2: stealing to keep cores busy. Developers have some control via
- 26:00Speaker 2: runtime dot gegomax prosess, but mostly it just.
- 26:03Speaker 1: Works okay, easy to start concurrent tasks. How do they
- 26:06Speaker 1: talk to each other?
- 26:07Speaker 2: Channels? Channels are the idiomatic way for guroteins to communicate safely.
- 26:11Speaker 2: They're typed conduits each each make caint you send with
- 26:16Speaker 2: change value and receive with value.
- 26:18Speaker 1: And they're syncretous by default, meaning send blocks until receive
- 26:22Speaker 1: and vice versa exactly.
- 26:24Speaker 2: For unbuffered channels, make chant ind it forces synchronization. They
- 26:27Speaker 2: operate fifo. You can also create buffered channels move fee,
- 26:32Speaker 2: make chan string ten.
- 26:33Speaker 1: Buffer channels let the sender proceed without a receiver immediately.
- 26:37Speaker 2: Available up to the buffer capacity. Yes, sends block only
- 26:40Speaker 2: if the butter is full, receives block only if it's empty.
- 26:43Speaker 1: How do you receive multiple values?
- 26:46Speaker 2: For range YEP for value rained loops until the channel
- 26:49Speaker 2: is closed to listen on multiple channels at once you
- 26:52Speaker 2: use select.
- 26:52Speaker 1: Select weights for the first channel.
- 26:54Speaker 2: That's ready right, select case vowel netwuble one cases, envel, default,
- 26:58Speaker 2: default if multiple are ready, uses one randomly. The default
- 27:01Speaker 2: case makes the select non blocking channels ensure only one
- 27:05Speaker 2: guaranteine receives each value sent. They're not message ques in
- 27:08Speaker 2: that sense.
- 27:08Speaker 1: Can you restrict channels to only sending or only receiving?
- 27:12Speaker 2: Yes, UNI directional channels, chantunt is send only, chance string
- 27:16Speaker 2: is receive only. Good for API clarity and type safety?
- 27:19Speaker 1: How do you signal you're done sending on a channel?
- 27:21Speaker 2: Receivers can detect a closed channel using the comma act
- 27:24Speaker 2: idiom value ok on. If ok is false, the channel
- 27:27Speaker 2: is closed and value is the zero value for the type.
- 27:31Speaker 1: What happens if you close an already closed channel or
- 27:33Speaker 1: send on a closed channel.
- 27:34Speaker 2: Panic Gotta be careful. Often you sink once or a
- 27:38Speaker 2: dedicated don channel pattern to ensure safe single closure.
- 27:42Speaker 1: Okay guarantines and channels for concurrency. What about managing the
- 27:46Speaker 1: life cycle of these operations, especially across APIs context exactly?
- 27:50Speaker 2: The context package is key for managing request life cycles, passing,
- 27:54Speaker 2: cancelation signals, deadlines, and request scoped values across API boundaries
- 27:58Speaker 2: and between guarantines.
- 27:59Speaker 1: What are the core parts?
- 28:00Speaker 2: Context dot background is the root empty context context dot
- 28:03Speaker 2: todo is a placeholder. Then you derive context using with cancel,
- 28:07Speaker 2: with deadline, with timeout, and with value.
- 28:09Speaker 1: And the context interface itself.
- 28:12Speaker 2: It is done a channel closed when the context is
- 28:14Speaker 2: canceled or times out. Aircast returns the reason for cancelation,
- 28:17Speaker 2: Context canceled or context deadline exceeded. Deadline tells you the
- 28:21Speaker 2: time it will be canceled, and value key retrieves associated values.
- 28:27Speaker 1: How do you attach values.
- 28:28Speaker 2: Context with value parent ctx key value. The key should
- 28:32Speaker 2: be comparable, often a custom unexported type to avoid collisions
- 28:36Speaker 2: between packages. Using the same string key value searches up
- 28:39Speaker 2: the context tree.
- 28:40Speaker 1: For the key and cancelation.
- 28:41Speaker 2: How does that work with cancel Parent Ctx returns a
- 28:44Speaker 2: new context and a cancel function. Calling cancel closes the
- 28:48Speaker 2: done channel that context and all contexts derived from it.
- 28:51Speaker 1: So gortein should listen on ctx.
- 28:53Speaker 2: Done yes, usually in a select statement select case natchy
- 28:56Speaker 2: ctx done, return ctx dot dr. It's crucial to defer
- 29:01Speaker 2: cancel right after getting the cancel function to ensure resources
- 29:04Speaker 2: are cleaned up. Timeouts and deadlines are just automatic ways
- 29:07Speaker 2: to call.
- 29:07Speaker 1: Cancel context seems essential for robust concurrent systems. How about
- 29:12Speaker 1: coordinating guarantines more directly synchronization.
- 29:15Speaker 2: The sink package has tools for that. Sinc weightgroup is
- 29:18Speaker 2: common for waiting for a group of gorgutines to finish.
- 29:21Speaker 1: How does it work?
- 29:22Speaker 2: You call WG dot ad DN for ngorutines. Each guarantine
- 29:27Speaker 2: calls WG done when finished, The main guarantine calls WG weight,
- 29:31Speaker 2: which blocks until the counter reaches zero. Need to be
- 29:34Speaker 2: careful not to misuse ad or done can cause deadlocks
- 29:37Speaker 2: or panics.
- 29:38Speaker 1: Is there something simpler for handling groups and errors?
- 29:41Speaker 2: Yes, go lang dot organ sensor group. It manages a group,
- 29:45Speaker 2: propagates context cancelation, and returns the first error encountered by
- 29:48Speaker 2: any groatine in the group. Often simpler than managing weight,
- 29:52Speaker 2: group and error channels manually.
- 29:54Speaker 1: What about protecting shared data from raise conditions?
- 29:56Speaker 2: That's sync mutex a mutual exclusion lock mutext dot lock
- 30:00Speaker 2: before accessing shared data. Mutex dot unlock afterwards insures only
- 30:04Speaker 2: one gorotine can be in the critical section at a time.
- 30:06Speaker 2: Don't copy utext's after first use. Trilock provides a non.
- 30:10Speaker 1: Blocking attempt and sync once.
- 30:12Speaker 2: Guarantees of function runs exactly once, even with concurrent calls
- 30:15Speaker 2: to its disdue method. Perfect for thread safe lazy initialization
- 30:19Speaker 2: like we mentioned for safely closing channels.
- 30:21Speaker 1: Good set of sync tools. Okay, last big area. Interacting
- 30:25Speaker 1: with the filesystem.
- 30:26Speaker 2: GO has several packages. Path filepath is for a platform
- 30:29Speaker 2: independent path manipulation, join xt der base and the AS
- 30:33Speaker 2: package that's for direct OS interaction. M adder ameshturl creates
- 30:38Speaker 2: parents to create files, open file, more control, remove all, recursive, delete,
- 30:43Speaker 2: stat file info, read file reads, whole file write file
- 30:47Speaker 2: reader also handles OS signals like interrupt.
- 30:50Speaker 1: What about the eye box package that's.
- 30:52Speaker 2: A newer abstraction layer for filesystems defines interfaces like fs,
- 30:56Speaker 2: dot FS and f smock file allows different filesystems implementations
- 31:00Speaker 2: like the real OS filesystem via OS dot d S
- 31:03Speaker 2: or in memory ones for testing using fistess dot map,
- 31:06Speaker 2: fs FS walkdor is great for traversing directory trees and.
- 31:09Speaker 1: Embedding files directly into the binary.
- 31:11Speaker 2: That's the embed package. Use the GO dot embed pattern
- 31:14Speaker 2: directive above a variable of type string, byte or mbed
- 31:17Speaker 2: dot f s. The compiler bundles the specified files or directories,
- 31:20Speaker 2: dot embed dot f S implements FS dot f S
- 31:23Speaker 2: so you can use the standard filesystem APIs to read
- 31:26Speaker 2: embedded files. Super handy for distributing assets with your app.
- 31:29Speaker 1: Wow. Okay, we've really covered the core of Go, from
- 31:32Speaker 1: structure and basics to concurrency and fileio.
- 31:35Speaker 2: Absolutely, it's a journey through modules, packages, types, variables, control flow, functions, interfaces, errors, generics, guaratines, channels, context, sync,
- 31:46Speaker 2: and the filesystem. Hopefully you have a clearer picture now
- 31:50Speaker 2: of how these pieces fit together and Go.
- 31:52Speaker 1: Yeah, definitely, and hopefully some of those specifics, like the
- 31:55Speaker 1: implicit interfaces or how gorteines work with the scheduler or
- 31:58Speaker 1: even embedding files. Hopefully those details spark some curiosity for
- 32:02Speaker 1: you listening Understanding those often leads to using the language
- 32:06Speaker 1: much more effectively.
- 32:08Speaker 2: And this deep dive is really just a starting point.
- 32:10Speaker 2: Go is a big language, always evolving. We definitely encourage
- 32:13Speaker 2: you to take this foundation and explore further, write some code,
- 32:17Speaker 2: dig into concurrency or generics if that interested you. The
- 32:20Speaker 2: official docs in the community are great resources.
- 32:22Speaker 1: So what aspects are you now curious about? What kinds
- 32:25Speaker 1: of problems might you try solving with Go? Using these fundamentals.
- 32:28Speaker 2: Yeah. Good Questions to ponder.
- 32:30Speaker 3: In a final thought, consider how goes designs that focus
- 32:33Speaker 3: on concurrency, the practical error handling, the efficient compilation. How
- 32:37Speaker 3: all that contributes to its success in modern software. How
- 32:40Speaker 3: might I understanding these GO fundamentals change how you approach
- 32:44Speaker 3: building software, even if you're using a different language. Something
- 32:47Speaker 3: to think about.