Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

TIL: Enabling Spell Checking in Vim

This command enables spell checking:

:setlocal spell spelllang=en_us

This enables the spell option and configures it to check for US English.

Unrecognized words are highlighted with one of the following:

	SpellBad	word not recognized			|hl-SpellBad|
	SpellCap	word not capitalised			|hl-SpellCap|
	SpellRare	rare word				|hl-SpellRare|
	SpellLocal	wrong spelling for selected region	|hl-SpellLocal|

Vim only checks for spelling; it does not perform grammar checks.

TIL: Password Hashing with Bcrypt in Go

You can use the bcrypt package for hashing passwords in Go. It also includes a cost parameter to increase the difficulty of the hashing algorithm, making it more resistant to brute-force attacks.

TIL 10: Structs in Go

Structs in Go

In Go, it is possible to embed structs within other structs:

type person struct {
    first string
    last string
    age   int
}

type employee struct {
    person
    employee_id int
    salary      int
}

We can instantiate the employee type as follows:

e := employee{
    person: person{
        first: "Ali",
        last:  "Yeşil",
        age:   33,
    },
    employee_id: 001,
    salary:      4500,
}

We can then access the fields of the embedded struct as if they were fields of the outer struct:

// e.first is a shorthand for e.person.first
fmt.Println(e.first) 

One can also use anonymous structs without explicitly defining a type:

z := struct {
    a string
    b int
}{
    a: "aaa",
    b: 123,
}

If these won’t be reused elsewhere, using anonymous structs can keep the code clean.

TIL 12: Modern C Features

🐢 There are some new features in C since I learned it in the 90s. One is variable-length arrays (VLAs). You can now set the length of an array at runtime. This makes some uses of pointers moot, but Linus Torvalds is reported to have said that the Linux kernel does not contain any VLAs.

🐇 It may be useful, though. It looks like syntactic sugar for a const pointer plus malloc, but implicitness is usually not ideal. It also appears to use the stack.

🐢 I haven’t looked into the details, but it may be useful in some cases. I don’t think it can replace pointer usage if it uses the stack, though. Stack sizes are usually small, and having arrays that can fill them up is not a good approach. Another feature is complex number support. It seems from the lecture that this is also a bit of a half-baked feature.

🐇 What is the difference between this and using a struct with two float fields?

🐢 There are operators (+, -, *, or ==) that support these. Since there is no operator overloading in C, having a separate complex number type may be useful.

🐇 The example looks like double complex cx = 1.0 + 3.0*I; and yes, this may be useful if you’re frequently using complex numbers in your code. But I think it’s unnecessary in most cases. There shouldn’t be such a frequent need for complex numbers, right?

🐢 A good complex number library will probably provide more than the built-in type, such as vectors for these numbers. A dedicated library will still be needed in most cases, I believe.

🐇 Another feature added to C is that struct members can now be initialized by name or index. You can initialize an array like int a[6] = { [3] = 29, [2] = 14 };, and it will initialize only those specific members.

🐢 Ah, this is much more useful than complex numbers for the general case.

🐇 There is also a span syntax: int a[10] = {1, 2, [2 ... 4] = 3, [5] = 30};.

🐢 That is really neat.

🐇 It’s also possible to omit the initial length, so the initializer values determine the size of the array. For example, int a[] = {1, 3, [30] = 55, [9090] = 1010}; will result in an array of length 9091.

🐢 How is this used with structs?

🐇 Instead of [], in struct initializers, we use the dot notation, like struct Point p1 = { .x=0, .y=10 };.

🐢 This is really useful and would keep much of the initialization code simpler.

🐇 There is also a way to initialize arrays of structs, like:

struct points pts[5] = { [0].x = 10, [0].y = 20, [3].x = 100 };

🐢 This feature is a nice addition to C; I really like it.