Basics Flashcards
To learn basics of golang
What does “package main” mean?
It lets the Go compiler know that we want this code to compile and run as a standalone program, as opposed to being a library that’s imported by other programs.
import “fmt”?
It imports the fmt (formatting) package from the standard library. It allows us to use fmt.Println to print to the console.
What’s “func main()”
It defines the main function, the entry point for a Go program.
Generally speaking, there are two kinds of errors in programming:
Compilation errors: Occur when code is compiled. It’s generally better to have compilation errors because they’ll never accidentally make it into production. You can’t ship a program with a compiler error because the resulting executable won’t even be created.
Runtime errors: Occur when a program is running. These are generally worse because they can cause your program to crash or behave unexpectedly.
Go code generally runs ____ than interpreted languages and compiles ____ than other compiled languages
faster, faster - Go programs don’t run quite as fast as its compiled Rust, Zig, and C counterparts. That said, it compiles much faster than they do, which makes the developer experience super productive. Unfortunately, there are no swordfights on Go teams…
Convert to int
temperatureFloat := 88.26
temperatureInt := int(temperatureFloat)
Rune in Golang
In the past, we only had one character set, and that was known as ASCII (American Standard Code for Information Interchange). There, we used 7 bits to represent 128 characters, including upper and lowercase English letters, digits, and a variety of punctuations and device-control characters. Due to this character limitation, the majority of the population is not able to use their custom writing systems. To solve this problem, Unicode was invented. Unicode is a superset of ASCII that contains all the characters present in today’s world writing system. It includes accents, diacritical marks, control codes like tab and carriage return, and assigns each character a standard number called “Unicode Code Point”, or in Go language, a “Rune”. The Rune type is an alias of int32. Important Points:
Always remember, a string is a sequence of bytes and not of a Rune. A string may contain Unicode text encoded in UTF-8. But, the Go source code encodes as UTF-8, therefore, no need to encode the string in UTF-8.
UTF-8 encodes all the Unicode in the range of 1 to 4 bytes, where 1 byte is used for ASCII and the rest for the Rune.
ASCII contains a total of 256 elements and out of which, 128 are characters and 0-127 are identified as code points. Here, code point refers to the element which represents a single value.
Recommended default types in go
When Go developers stray from the “default” type for any given type family, the code can get messy quickly. Unless you have a good performance related reason, you’ll typically just want to use the “default” types:
bool
string
int
uint
byte
rune
float64
complex128
When should I use a more specific type?
When you’re super concerned about performance and memory usage.
That’s about it. The only reason to deviate from the defaults is to squeeze out every last bit of performance when you are writing an application that is resource-constrained. (Or, in the special case of uint64, you need an absurd range of unsigned integers).
Explain “Go is Statically Typed language”
Go enforces static typing meaning variable types are known before the code runs. That means your editor and the compiler can display type errors before the code is ever run, making development easier and faster.
Contrast this with most dynamically typed languages like JavaScript and Python… Dynamic typing often leads to subtle bugs that are hard to detect. The code must be run to catch syntax and type errors. (sometimes in production if you’re unlucky 😨)
Go - Small memory footprint?
Go programs are fairly lightweight. Each program includes a small amount of extra code that’s included in the executable binary called the Go Runtime. One of the purposes of the Go runtime is to clean up unused memory at runtime. It includes a garbage collector that automatically frees up memory that’s no longer in use.
Constants declaration in go
Constants are declared with the const keyword. They can’t use the := short declaration syntax.
Example:
const pi = 3.14159
Constants can be primitive types like strings, integers, booleans and floats. They cannot be more complex types like slices, maps and structs.
Top 5 Verbs for Sprintf
%v
It takes pretty much any variable type and formats it in the default style for that type. Unfortunately, since it is not as precise about what it does, it’s used less in production code. However, it is great for early-stage development, experimenting, and debugging.
%s
Simply a string. This verb doesn’t interpret the string that’s passed in and so it shows up as is.
%q
This verb formats values (i.e., arguments) with quotes. This is handy when you need quoted and quote-escaped values.
%d
%d formats a regular old base-10 integer number.
%t
Presumably ‘t’ here stands for true as %t formats a boolean value.
What is “func sub(x int, y int) int” is known as
the “function signature”.
go runtime use
Its part of created binary from your code. But what does it do,
- memory management
- concurrency
- built in data structures
waitgroups
Golang Waitgroup allows you to block a specific code block to allow a set of goroutines to complete execution. An example would be to block the main function until the goroutines are completed and then unblocks the group.