Skip to content

How to avoid shadowing builtins? #164

@abought

Description

@abought

While attempting to follow this layout new to Go, I created a file named cmd/api/main.go.

This lead to some quite unexpected behavior: go build -a -v cmd/api/ produced a working binary that did nothing (and ignored my main method). I got the expected result by changing the build command to reference either of ./cmd/api or cmd/api/main.go

After doing some digging, it was suggested that I had accidentally referenced a (quasi-hidden?) standard package with a similar name: https://pkg.go.dev/cmd/api Hence, I was getting a "binary" that successfully built, but not for my code.

I hadn't expected that the standard project layout would choose a folder (cmd) that might allow collisions with stdlib/ existing packages. Perhaps this guide could be extended with advice on how to avoid (or detect) when this is happening?

(I recognize that it's hard to be future proof, but this bit me in real usage on Day 1, while experimenting with Go on a boilerplate project with a friend)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions