Skip to content

46

Test Double is a generic term for any case where you replace a production object for testing purposes.

Under test doubles, there are various types like stubs, spies and indeed mocks! Check out Martin Fowler’s post for more detail.

45

When creating an array in Go, we can put ... in place of array’s length to determine the length of the array automatically based on the number of elements provided.

nums := [...]int{1, 2, 3} // [3]int{1, 2, 3}

44

When working with session cookie, it’s important to be aware that using SameSite=Strict will block the session cookie being sent by the user’s browser for all cross-site usage — including safe requests with HTTP methods like GET and HEAD.

While that might be the safest setting, the downside is that the session cookie won’t be sent when a user clicks on a link to your application from another website. Your application will initially treat the user as ‘not logged in’ even if they have an active session.

So if your application will potentially have other websites linking to it (or even links shared in emails or private messaging services), then SameSite=Lax is generally the more appropriate setting.

43

Javascript compatible valid email regex recommended by W3C and Web Hypertext Application Technology Working Group.

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

42

In Go, the syntax for SQL parameter placeholder differs depending on your database. MySQL, SQL Server and SQLite use the ? notation, but PostgreSQL uses the $N notation.

For example:

-- MySQL
INSERT INTO ... VALUES (?, ?, ?);
-- PostgreSQL
INSERT INTO ... VALUES ($1, $2, $3);

41

To remove a package in a Go project:

  • Run go get and postfix the package path with @none.

    Terminal window
    $ go get github.com/path/to/package@none
  • Remove all references to the package in your code, then run go mod tidy, which will automatically remove any unused packages from your go.mod and go.sum files.

    Terminal window
    $ go mod tidy -v

40

A Tailwind snippet to emulate .stretched-link from Bootstrap

<a class="before:absolute before:inset-0 before:z-0 before:overflow-hidden before:whitespace-nowrap before:indent-[100] before:content-['']">link</a>

Note:

  • Make sure the parent element has position relative class
  • Make sure the z-index value of the other nested links are bigger than 0

39

Analogy of how OAuth works:

At a high level, the process is similar to sending a child home from school with a permission slip to go on a field trip. The teacher asks students if they want to go on a field trip. If they say yes, she sends them home with a paper for their parents to sign. Their parents then sign the permission slip and send the child back to school with it. Once the teacher receives the signed permission slip, the child can be taken on the field trip.

38

Quickly write to a file through command line:

  • Replace whole file
    Terminal window
    echo "Hello world" | cat > hello.txt
  • Append to end of file
    Terminal window
    echo "Hello world" | cat >> hello.txt

37

A CSS snippet to handle nested border radius:

.parent {
--nested-radius: calc(var(--radius) - var(--padding));
}
.nested {
border-radius: var(--nested-radius);
}