Location>code7788 >text

golang assigns 0o755 instead of 0755.

Popularity:368 ℃/2024-08-05 00:52:48

 

In Go, it is a recommended practice to use the prefix 0o to explicitly indicate octal numbers when specifying file permissions.


This is because in the Go language, octal literals must begin with 0o or 0O followed by an octal number (0-7).

This syntax was introduced in Go 1.8 to minimize confusion between octal literals and decimal numbers starting with zero.

 

In earlier versions of Go, or in some other programming languages such as C, C++, and Shell scripts, octal literals usually begin with just zero (0), such as 0755.

However, this representation can cause problems in Go, because starting with Go 1.0, numbers starting with zero are parsed as decimal numbers by default, unless it is immediately followed by an octal number (0-7).

To avoid ambiguity, the Go language designers decided to introduce the 0o prefix to unambiguously identify octal numbers.

 

Therefore, in Go, the correct file permission constant should be written 0o755, which means that the owner has read, write, and execute permissions (rwx), while groups and other users only have read and execute permissions (rx).

If you use 0755, while the compiler will correctly infer that this is an octal number in most cases, this usage is not in line with Go language best practices and can lead to incorrect parsing in some cases.

 

  • Text to Picture Online
  • Image Conversion Base64
  • Website Technology Stack Testing
  • Domain name registration information inquiry

Link:/farwish/p/18342473