Home > Backend Development > Golang > How Should Go Filenames and Struct Placement Adhere to Go's Naming Conventions?

How Should Go Filenames and Struct Placement Adhere to Go's Naming Conventions?

Linda Hamilton
Release: 2024-12-09 20:46:15
Original
436 people have browsed it

How Should Go Filenames and Struct Placement Adhere to Go's Naming Conventions?

Go File Naming Conventions

In Go, package naming conventions dictate the use of lowercase words without underscores. However, the question arises whether these conventions extend to filenames. Additionally, should one struct be placed in a single file, similar to a Java class, with the filename reflecting the struct name?

The Go community has established several guidelines for filenames:

  • Filenames starting with "." or "_" are omitted by the go tool.
  • Files with the suffix "_test.go" are reserved for compilation and execution by the "go test" tool.
  • Files with operating system or architecture-specific extensions inherit those restrictions. For instance, "name_linux.go" will only build on Linux, while "name_amd64.go" will only build on amd64 (equivalent to adding a "// build amd64" line at the file's beginning).

For the example provided, a struct named "WebServer" should be placed in a file named "web_server.go" as follows the convention of lowercase words separated by underscores.

Refer to the official Go documentation (https://pkg.go.dev/cmd/go) for further details on filename conventions.

The above is the detailed content of How Should Go Filenames and Struct Placement Adhere to Go's Naming Conventions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template