skip to content
Alvin Lucillo

Loading a package without using it in Go

/ 1 min read

💻 Tech

In Go, there’s a technique to only load the package without using it. This is useful when you want to run the init function of the package. For example, if you want to load a postgresql driver, you can do this:

import (
    _ "github.com/lib/pq"
)

With that, sqlx.DB will be able to use the postgresql driver and you can connect to the database.

🧩 Puzzle

This is derived from a Brilliant.org puzzle. You entered a room and saw three boxes with these labels:

  • Box 1: This contains 100 shirts
  • Box 2: This contains 50 shirts and 50 pants
  • Box 3: This contains 100 pants

You also saw a note saying “Each box is labeled incorrectly”.

The goal is to determine which box contains 100 shirts. You can only pick one item from one box. Which box will you pick? A, B, or C?