Type Conversion & struct tgs

We’ve already mentioned that struct tags are ignored when doing conversion between struct types. Now we see where that’s defined, along with an example:Conversions …Struct tags are ignored when comparing struct types for identity for the purpose of conversion:type Person struct { … | Continue reading


@boldlygo.tech | 10 months ago

Conversion of type parameters

Today I’ll be live-streaming again, doing TDD on an open-source project. I hope you can join! Conversions … Additionally, if T or x’s type V are type parameters, x can also be converted to type T if one of the following conditions applies: Both V and T are type parameters and a v … | Continue reading


@boldlygo.tech | 10 months ago

Non-constant conversions, part 2

Today we’ll finish the list of non-constant conversion rules that don’t relate to type parameters. Conversions … x’s type and T are both integer or floating point types. x’s type and T are both complex types. x is an integer or a slice of bytes or runes and T is a string type. x … | Continue reading


@boldlygo.tech | 10 months ago

Non-constant conversions

Yesterday we looked at conversions of constants. Let’s now consider the more common conversion scenarios of variables and other non-constant expressions. Conversions … A non-constant value x can be converted to type T in any of these cases: x is assignable to T. ignoring struct t … | Continue reading


@boldlygo.tech | 10 months ago

Conversion of constants

In case you missed it… It has come to my attention that Monday’s livestream went to the wrong stream on YouTube! This means if you followed the link I shared with you on Monday, you likely sat there waiting for 2 hours, only to be disappointed by a lack of live programming. Alas … | Continue reading


@boldlygo.tech | 10 months ago

Ambiguous conversion expressions

There aren’t many places where Go syntax is ambiguous, but here’s one:Conversions …If the type starts with the operator * or <-, or if the type starts with the keyword func and has no result list, it must be parenthesized when necessary to avoid ambiguity:*Point(p) // same as *(P … | Continue reading


@boldlygo.tech | 10 months ago

Conversions

On todays’s livestream, we’ll be Pair Programming on a real project. I hope to see you there! Conversions A conversion changes the type of an expression to the type specified by the conversion. A conversion may appear literally in the source, or it may be implied by the context i … | Continue reading


@boldlygo.tech | 10 months ago

Receive operator

Channels are deceptively simple. They seem a bit magical. They feel like they do something. They feel like they send data around your system. But they really don’t. They’re just data types. They’re probably best thought of like Go’s array or slice type, with the limitation that y … | Continue reading


@boldlygo.tech | 10 months ago

Live pair programming on a real project

Mark your calendar! If you’ve ever been curious about pair programming, and want to see it in action on a real project, not just a coding exercise, you’ll want to join Monday’s livestream. I’ll be doing some live Pair Programming with Denis Čahuk and Adrian Stanek. Denis and Adri … | Continue reading


@boldlygo.tech | 10 months ago

Address operators

Let’s talk about addresses.Address operators For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressa … | Continue reading


@boldlygo.tech | 10 months ago

Logical Operators

Logical operators Logical operators apply to boolean values and yield a result of the same type as the operands. The left operand is evaluated, and then the right if the condition requires it. && conditional AND p && q is "if p then q else false" || conditional OR p || q is "if p … | Continue reading


@boldlygo.tech | 10 months ago

Introducing Go 1.22!

Join me in less than an hour, when I’ll be live streaming again! I’ll be continuing to add SQLite support to Kivik, using Test-Driven Development.Go 1.22 was released on February 7. So I’m a bit behind on talking about the changes introduced to the spec. But better late than neve … | Continue reading


@boldlygo.tech | 10 months ago

Comparison operators, conclusion

Comparison operators …Slice, map, and function types are not comparable. However, as a special case, a slice, map, or function value may be compared to the predeclared identifier nil. Comparison of pointer, channel, and interface values to nil is also allowed and follows from the … | Continue reading


@boldlygo.tech | 10 months ago

Comparison operators, part IV

Comparison operators …Struct types are comparable if all their field types are comparable. Two struct values are equal if their corresponding non-blank field values are equal. The fields are compared in source order, and comparison stops as soon as two field values differ (or all … | Continue reading


@boldlygo.tech | 11 months ago

Comparison operators, part III

Today we continue through the list of data types and how comparison and ordering works on each.Comparison operators …Channel types are comparable. Two channel values are equal if they were created by the same call to make or if both have value nil. Notice that channel comparison … | Continue reading


@boldlygo.tech | 11 months ago

Comparison operators, continued

Yesterday we started on comparison operators. Today we’ll continue, but there are a lot of types to cover, so I’ll break this down into a two or three parts, to keep each day’s email easily digestible.Recall that we had just been introduced to the concepts of ordered and comparab … | Continue reading


@boldlygo.tech | 11 months ago

Comparison operators

I’ll be livestreaming again today! I hope you can join me as I continue where I left off last week, adding some new features to my open-source library, Kivik!Let’s talk about a mundane detail… that actually has some interesting nuances: Comparisons!Comparison operators Comparison … | Continue reading


@boldlygo.tech | 11 months ago

String concatenation

Today we’re looking at a deceptively short section of the spec: String concatenation… String concatenation Strings can be concatenated using the + operator or the += assignment operator: s := "hi" + string(c) s += " and good bye" String addition creates a new string by concatenat … | Continue reading


@boldlygo.tech | 11 months ago

Floating-point operators

Floating-point operators For floating-point and complex numbers, +x is the same as x, while -x is the negation of x. The result of a floating-point or complex division by zero is not specified beyond the IEEE-754 standard; whether a run-time panic occurs is implementation-specifi … | Continue reading


@boldlygo.tech | 11 months ago

Integer overflow

Integer overflow For unsigned integer values, the operations +, -, *, and << are computed modulo 2n, where n is the bit width of the unsigned integer’s type. Loosely speaking, these unsigned integer operations discard high bits upon overflow, and programs may rely on “wrap around … | Continue reading


@boldlygo.tech | 11 months ago

Can you think of an use case for ^x?

Last week I talked about unary integer operators, including the ^ operator. A reader wrote back asking: Can you think of an use case for ^x? So today I’m going to answer this question! No! LOL Not very satisfying, is it? So I did some digging to find some examples. Note, none of … | Continue reading


@boldlygo.tech | 11 months ago

You're already running my Code

Last week I mentioned I’d be speaking at FOSDEM over the weekend. Well, the weekend is over, and the video of my presentation is now live, so you can catch it even if you weren’t in Belgium. You can also catch many of the other great Go-related talks (and more coming every day, a … | Continue reading


@boldlygo.tech | 11 months ago

FOSDEM 2024: You're already running my code in production

Boldly Go Open main menu Blog YouTube Hire Me Contact Daily Email Boldly Go Close menu Blog YouTube Hire Me Contact Daily Email FOSDEM 2024: You're already running my code in production February 3, 2024 How I became a Go contributor, and you can, too. My Content Boldly Go: Daily … | Continue reading


@boldlygo.tech | 11 months ago

Unary integer operators

We have a quick one today to finish up integer operators, before diving into a semi-hairy topic next…Integer operators …For integer operands, the unary operators +, -, and ^ are defined as follows:+x is 0 + x -x negation is 0 - x ^x bitwise complement is m ^ x with m = "all bits … | Continue reading


@boldlygo.tech | 11 months ago

Divide by zero, and shifting

Integer operators …If the divisor is a constant, it must not be zero. If the divisor is zero at run time, a run-time panic occurs.I’m sure you expected that. Division by zero is pretty universally not allowed.var a = 3 var b = 0 var c = a / 0 // Won't compile var d = a / b // run … | Continue reading


@boldlygo.tech | 11 months ago

Integer operators

Today we continue our discussion of arithmetic operators, with a topic that is likely not new to you at all: Integer operators.Integer operators For two integer values x and y, the integer quotient q = x / y and remainder r = x % y satisfy the following relationships:x = q*y + r … | Continue reading


@boldlygo.tech | 11 months ago

Join me at FOSDEM

Do you have plans to be in Brussels this weekend? If so, join me at FOSDEM 2024! I’ll be speaking in the Go Devroom on Saturday morning on my journey to becoming a Go contributor. Not going to be in Brussels? Follow me on Mastodon where I’ll be live micro-blogging from the event, … | Continue reading


@boldlygo.tech | 11 months ago

Arithmetic operators

Arithmetic operators Arithmetic operators apply to numeric values and yield a result of the same type as the first operand. The four standard arithmetic operators (+, -, *, /) apply to integer, floating-point, and complex types; + also applies to strings. The bitwise logical and … | Continue reading


@boldlygo.tech | 11 months ago

Operator precedence

I hope we all know what operator precedence means… but just in case it’s fuzzy, I’ll illustrate with a simple example from junior high school. What does this mean? 1 + 2 * 3 It’s either 9 or 7, right? It depends on the order in which we apply the + and * operations. I’m sure most … | Continue reading


@boldlygo.tech | 11 months ago

Shift operators

Operators …The right operand in a shift expression must have integer type or be an untyped constant representable by a value of type uint.A bunch of examples demonstrating this follow in the spec, so I won’t provide my own.… If the left operand of a non-constant shift expression … | Continue reading


@boldlygo.tech | 11 months ago

Operators

Operators Operators combine operands into expressions.Nice. I love a concise description/definition. Especially after the confusion that was yesterday’s post about loose and exact unification.Expression = UnaryExpr | Expression binary_op Expression . UnaryExpr = PrimaryExpr | una … | Continue reading


@boldlygo.tech | 11 months ago

Exact & loose type unification

Type unification … Unification uses a combination of exact and loose unification depending on whether two types have to be identical, assignment-compatible, or only structurally equal. The respective type unification rules are spelled out in detail in the Appendix. The precise de … | Continue reading


@boldlygo.tech | 11 months ago

Reviewing an open-source project

Oops! I went on a vacation last week. But so did my deployment pipeline. Which means that the emails I had queued up to send to you didn’t go through. So this week enjoy what I wrote for last week. And I hope you enjoyed a short vacation from me in your inbox, too. I just publish … | Continue reading


@boldlygo.tech | 11 months ago

Type unification

A couple of days ago we saw the spec reference the concept of “type unification”. Today we start through that explanation…. Type unification Type inference solves type equations through type unification. Type unification recursively compares the LHS and RHS types of an equation, … | Continue reading


@boldlygo.tech | 12 months ago

Successful type inference

Today we finish up the discussion on type inference. So we’ve now Type inference … If the two phases are successful, type inference determined a type argument for each bound type parameter: Pk ➞ Ak A type argument Ak may be a composite type, containing other bound type parameters … | Continue reading


@boldlygo.tech | 12 months ago

Error handling in Go web apps shouldn't be so awkward

A quick interruption to our trek through the Go spec, to mention a new long-form article I just wrote. Jump straight to the full articleIn this post I’m going to describe an error-handling pattern I’ve found to be fairly elegant when writing REST, gRPC, or other services in Go. I … | Continue reading


@boldlygo.tech | 1 year ago

Precedence of type inference

Type inference …Type inference gives precedence to type information obtained from typed operands before considering untyped constants. Therefore, inference proceeds in two phases:The type equations are solved for the bound type parameters using type unification. If unification fa … | Continue reading


@boldlygo.tech | 1 year ago

The many type equations

Type inference …Type inference supports calls of generic functions and assignments of generic functions to (explicitly function-typed) variables.So just to call out this point, made in passing: You cannot assign the result of a generic function to a generic, non-instantiated type … | Continue reading


@boldlygo.tech | 1 year ago

Bound type parameters

Type inference …Given a set of type equations, the type parameters to solve for are the type parameters of the functions that need to be instantiated and for which no explicit type arguments is provided. These type parameters are called bound type parameters. For instance, in the … | Continue reading


@boldlygo.tech | 1 year ago

Error handling in Go web apps shouldn't be so awkward

Updated 2024-01-10 to include Domain-specific errors and limitations sections. In this post I’m going to describe an error-handling pattern I’ve found to be fairly elegant when writing REST, gRPC, or other services in Go. I have three goals in writing this post: To explain the pa … | Continue reading


@boldlygo.tech | 1 year ago

Type equations

I don’t have a lot to expand on in this section, except to offer a pre-amble about some technical terms and symbols that won’t be familiar to everyone: ≡ is a symbol from mathematics that means “identical to”. It’s similar to = which we’re all famliar with, but “stricter”. The sp … | Continue reading


@boldlygo.tech | 1 year ago

Type inference

We just covered instantiations, and learned that it is often possible to infer generic types. Nowe we’ll examine how that mechanism works. This bit gets a bit into the weeds. You’ll be forgiven if you choose to skip over this.Type inference A use of a generic function may omit so … | Continue reading


@boldlygo.tech | 1 year ago

Partial type argument lists

Instantiations … A partial type argument list cannot be empty; at least the first argument must be present. The list is a prefix of the full list of type arguments, leaving the remaining arguments to be inferred. Loosely speaking, type arguments may be omitted from “right to left … | Continue reading


@boldlygo.tech | 1 year ago

When function instantiations can be inferred

A quick update on my livestream: It’s on hold until February, as my family and I made a bit of a last-minute trip to visit family for the month of January, so I won’t be in my studio for a while. Instantiations … When using a generic function, type arguments may be provided expli … | Continue reading


@boldlygo.tech | 1 year ago

How could you improve with a personal Go mentor?

I started Boldly Go: Daily in early January of this year. This means that if you’re reading this, you’re one of the hundreds of people who have joined this daily list in 2023. Thanks! I sincerely hope you’re learning from it.It’s my mission to make Go approachable to people of al … | Continue reading


@boldlygo.tech | 1 year ago

Instantiations

Instantiations A generic function or type is instantiated by substituting type arguments for the type parameters. Instantiation proceeds in two steps:Each type argument is substituted for its corresponding type parameter in the generic declaration. This substitution happens acros … | Continue reading


@boldlygo.tech | 1 year ago

Passing slices to variadic functions

Passing arguments to ... parameters …If the final argument is assignable to a slice type []T and is followed by ..., it is passed unchanged as the value for a ...T parameter. In this case no new slice is created.Given the slice s and calls := []string{"James", "Jasmine"} Greeting … | Continue reading


@boldlygo.tech | 1 year ago

Passing arguments to ... parameters

If you’ve been a reader for a while, you may recall back in April when I first talked about variadic functions. Now we’re ready to dig in a bit further to the details of how these mysterious creatures work.First we’ll see how they work “from the inside”. That is, from the perspec … | Continue reading


@boldlygo.tech | 1 year ago