L5 · LanguagesProgramming Languages 101 · part 6 of 6

Why Go Rewrites 0644 as 0o644 (and Why chmod Won't Take 'rwx')

I typed 0644, saved, and my editor changed it to 0o644. That lowercase o fixes a kind of bug that is fifty years old, and it also explains why file permissions are numbers and not letters.

Takumi Yamada
· 6 min read · 2 references
gooctalfile-permissionschmodunixprogramming-languages

While I was adding file saving to my Go todo app, I wrote a very ordinary line.

os.WriteFile(filename, data, 0644)

I saved the file. My editor ran gofumpt, a stricter version of gofmt, Go's standard formatting tool, and the line changed without asking me.

os.WriteFile(filename, data, 0o644)

I didn't type that o. My first thought was that a plugin was damaging my file.

It wasn't. The formatter changed the number on purpose. The reason goes back to a design mistake from about fifty years ago, which Go has now fixed.

The number that looks like ten

In the early 1970s, C made a rule that almost every language since has copied. A whole number written with a zero in front is octal, which means base 8. The zero isn't decoration. It changes the base.

var a = 010 // Reads like ten. Compiles as eight.

That's the whole trap. In base 8, 010 means one eight and no ones, which is eight. Your eye is trained to skip a leading zero, because everywhere else a leading zero is only padding: in postcodes, in invoice numbers, or in the number 07 on a football shirt.

READS LIKEdecimal010tensonespadding1 × 10 + 0 × 1 = 10COMPILES ASoctal010eightsonesbase 81 × 8 + 0 × 1 = 8the leading zero isnot a digit here,it sets the base
The literal 010, read both ways. The leading zero is what changes the base.

This has caused real bugs for decades. Someone lines up a column of numbers by adding zeros in front, and some of them silently become base 8. JavaScript's strict mode finally made the old form an error.

C only chose this syntax because was the natural way to write numbers on the PDP-8 and PDP-11, the computers Unix was first written for. On that hardware, were grouped in threes.

The front panel of a PDP-11: rows of lights for the address and the data, and below them a row of switches numbered 17 down to 0, coloured blue and red in groups of three.
The front panel of a PDP-11 from 1972. Each switch sets one bit, and the switches are coloured in groups of three, so each coloured group is one octal digit. An operator entered an address or a value by reading it as octal and setting the switches one group at a time.Photo: Wolfgang Stief, CC0 1.0, via Wikimedia Commons (original). Changes: cropped and resized.

Go 1.13: the prefix family gets its missing member

A prefix at the start of a number can say which base it's in. Go has three of them today.

PrefixBaseExample
0bbinary0b1010
0xhexadecimal0xFF
0ooctal0o644

Go inherited 0x from C. Go 1.13, released in 2019, added 0b for binary and 0o for octal. With 0o, octal is written out clearly instead of being hinted at by a zero.

decimal4204 hundreds, 2 tens, 0 ones= 420binary0b110100100256 + 128 + 32 + 4= 420octal0o6446 × 64, 4 × 8, 4 × 1= 420hexadecimal0x1A41 × 256, 10 × 16 (written A), 4 × 1= 420
One number written with each prefix. The prefix tells the reader which base the digits are in.

The old form with a leading zero still works, because fifty years of C habits don't disappear in one release. Plain gofmt leaves it alone. gofumpt, which many editor setups run instead, treats 0o644 as the correct spelling in any module that uses Go 1.13 or later, and rewrites the old form.

The treats 0644 and 0o644 exactly the same. So the rewrite doesn't change your program. It changes what the next person reading the code needs to know. To read 0644 correctly, you have to remember a hidden rule from 1972. 0o644 tells you directly.

This is typical of Go. When I wrote about coming to Go from C++, I saw the same pattern again and again. Where C leaves a danger to style guides, Go changes the syntax so that the dangerous code can't be written. The declaration order, the required braces, and now the octal prefix all follow the same idea.

Fine, but why is a file permission octal at all?

Because a Unix permission is nine bits, and nine bits fit exactly into three octal digits.

Every file has permissions for three groups of people: the owner, the file's group, and everyone else. Each of the three gets three bits: read, write and execute. One octal digit holds exactly three bits, so each group gets one digit, with nothing left over.

0obitsallowsworthOWNER61r41w20x1GROUP41r40w20x1OTHER41r40w20x1
0o644: one digit per tier, three bits per digit.

Each permission always has the same value.

LetterPermissionValue
rread4
wwrite2
xexecute1

Add up the values for one group, and you get that group's digit. Read and write is 4 + 2 = 6. Read and execute is 5. Read alone is 4. All three is 7.

So 0o644, the usual mode for ordinary files, means the owner can read and write, and everyone else can only read. 0o755, the usual mode for programs and directories, means the owner can do everything, and everyone else can read and run but not change.

In or decimal, the three groups don't line up with the digits. Octal is the only base where each digit is exactly one group. The permission system wasn't adapted to octal later. It was designed in octal, on those same PDP-11s.

Why can't I just write "+rwx"?

This was my next question, because in the terminal I never think in numbers.

chmod +x deploy.sh
chmod u+rw notes.txt

So why does Go make me do arithmetic, when chmod accepts readable letters?

Because the letters aren't the 's language. They are only how the chmod program talks to you.

chmod is a small program. When you give it u+rw, it does what you would do by hand. It reads the letters, looks up the file's current permissions, works out the new nine-bit number, and passes that number to the . The letters never leave the chmod program. The system call underneath, which is how a program asks the kernel to do something, only accepts a number.

KERNELtakes an integerCHMODa small programu+rwletterschmodworks out the number9 BITSa numberGOos.WriteFile0o644a numberno parser in between
chmod accepts letters because it converts them itself. Go asks for what the kernel asks for.

Go's os.WriteFile sits just above that same , so it asks for what the kernel asks for, which is a number. The type is an unsigned 32-bit integer with the permission bits in the lowest nine places, under its own name.

type FileMode uint32

There is no code anywhere on that path that reads letters. Go chose not to put a translator between you and the operating system.

It does give you one help in the other direction. FileMode has a String() method, so printing a mode shows the familiar letters from ls -l.

fmt.Println(os.FileMode(0o644)) // -rw-r--r--
fmt.Println(os.FileMode(0o755)) // -rwxr-xr-x

Numbers go in and letters come out. The letters are for people. The number is what the kernel uses.

The three you will actually use

Almost every time a Go program sets a permission, it's one of these three.

0o644 is for data files your program writes, such as configs, CSV files and logs. The owner can change them, and everyone can read them.

0o600 is the same, but private. Use it for tokens, keys and anything secret. Only the owner can read or write.

0o755 is for directories and for anything that is meant to be run. A directory needs the execute bit, because that bit is what lets you open it.

OWNERGROUPOTHER0o644data filesrw-r--r--the owner edits,everyone else reads0o600secretsrw-------the owner only0o755programs, directoriesrwxr-xr-xthe owner does everything,everyone else reads and runs
The three modes spelled out. Each octal digit is one group of three boxes.

For the todo app's save file, 0o644 is the correct choice.

Wrapping Up

When gofumpt changes 0644 into 0o644, your editor isn't misbehaving. The formatter is removing one of the oldest sources of bugs in programming, every time you save.

And the permission is a number because Go's function sits closer to the kernel than chmod does. There are three groups, three bits each, and one octal digit per group, the same as on the PDP-11.

If Go is new to you, Go 101 covers the language from the beginning. If you are coming from C++ like I did, the pointers, slices, and errors post covers the bigger differences.

Check yourself

3 questions · nothing is saved
  1. 1.In C, and in Go's old style, what number is 010?
  2. 2.What does the mode 0o644 let people do with a file?
  3. 3.chmod accepts u+rw. Why does os.WriteFile want a number instead?

References & Primary Sources

Official specifications, standards, and documentation cited in this post.

  1. [1]

    Defines prefix notation for binary (0b/0B), octal (0o/0O), and hexadecimal (0x/0X) literals introduced in Go 1.13.

    SPECgo.dev
  2. [2]

    POSIX specification for file permission bitmasks, S_IRUSR, S_IWUSR, and octal mode representation.

    STANDARDpubs.opengroup.org

Who wrote this

Author profile and background

Takumi Yamada

Takumi Yamada

· Backend Engineer

Backend engineer building services, databases, Linux tooling, and NEGEN SYSTEMS. Writing about low-level mechanics, distributed systems, and real-world security.

End of post
NEGEN SYSTEMS
Receipt · post 25 of 31
Written
Jul 7, 2026
Length
1,218 words · 6 min
Built
…
Served by
…
Delivered
…
Over the wire
…
Go's share
…
Next up the stack · L6 Engineering

Software Jargon, Decoded: Every Term Developers Throw Around

Idempotent, race condition, tech debt, LGTM, yak shaving: the everyday words of software teams in one place, with what each one actually means and why it exists.

18 min read