Backend Development
Golang
Go language file naming convention: special processing of files starting with an underscore or dot
Go language file naming convention: special processing of files starting with an underscore or dot

File identification mechanism for Go build tools
In the development practice of Go language, file names usually follow certain conventions. However, the go build command will take a special treatment for Go source files whose file names begin with an underscore (_) or a period (.): they will be completely ignored and will not participate in compilation as a valid part of the package. This means that even if exported functions or types are defined in these files, they cannot be accessed by other files within the same package or by other code that imports the package.
This behavior is not an arbitrary decision of the go toolchain, but is clearly defined by the go/build package in the Go standard library. The go/build package is responsible for parsing the structure and dependencies of Go packages. Its internal logic clearly states that when scanning a package directory, the following types of files will be excluded:
- .go files in the package documentation.
- Files whose file names begin with _ or . (generally considered editor temporary files or auxiliary files that should not participate in the build).
- Files that do not meet the build constraints of the current build environment (e.g. operating system, architecture, etc.).
The original intention of this design choice is to facilitate developers to manage auxiliary files in the project, such as:
- Temporary files generated by the editor (such as _temp.go, .vscode.go).
- Draft files or configure scripts that the user wishes to exclude from the build process.
Practical impact and examples
When a Go package contains source files starting with an underscore or dot, their contents will not be compiled into the final executable or library. For example, consider the following project structure:
mypkg/ _internal_helper.go // This file will be ignored by go build .config_data.go // This file will also be ignored by go build api.go // This file will be included in the build utils.go // This file will be included in the build
If a function InternalFunc() is defined in _internal_helper.go, then trying to call mypkg.InternalFunc() in api.go or utils.go will result in a compilation error because the compiler cannot find the function.
Distinguish special purpose documents
It should be noted that there are some file name conventions in the Go language that also contain underscores, but they are handled differently from the above, for example:
- Test file (_test.go) : such as my_package_test.go. Such files will be compiled and run when executing the go test command, but will be ignored in the regular go build command. The underscore here is not the first character of the file name.
- Build constraint files (_os.go, _arch.go) : such as network_unix.go or data_windows.go. Such files are included in the build in certain environments and ignored in others, depending on the operating system or architecture their suffix matches. Likewise, the underscore here is not the first character of the file name.
The core difference is that the go build command only ignores Go source files whose file names begin with an underscore or a period. For the case where an underscore appears in the middle of a file name, the Go compiler will handle it according to its specific semantics (such as test files, build constraints).
Things to note
- Avoid for core code : The core logic of a package or any code expected to be compiled and used should never be placed in a Go source file that starts with an underscore or dot.
- Make good use of the exclusion mechanism : If you really need to exclude certain Go files from the build, such as as drafts, temporary scripts, or auxiliary files for specific development tools, using _ or . as a prefix is an effective and Go-compliant approach.
- Understand build constraints : Distinguish between hard exclusion rules for underscores/dots at the beginning of file names and conditional inclusion/exclusion rules based on specific semantics such as _test.go and _os.go.
Summarize
The go build command of the Go language adopts an explicit ignore policy for Go source files whose file names begin with an underscore or dot. This is designed to simplify project management and exclude temporary files or non-build code. As a Go developer, it is crucial to understand and follow this file naming convention to ensure that your code compiles, packages, and runs correctly and avoids potential problems caused by unrecognized files.
The above is the detailed content of Go language file naming convention: special processing of files starting with an underscore or dot. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20521
7
13634
4
How to apply the facade pattern (Facade) in Golang Go language simplifies the API of complex systems
Mar 10, 2026 pm 12:27 PM
The Facade should be used when the caller needs to write more than 5 lines of initialization, call more than 3 packages in sequence, and manually handle intermediate states; it should pass the interface rather than the specific type, provide 3 to 5 core methods, and enforce Shutdown() resource cleanup.
SQL performance analysis tool in Golang web development Go language GORM-Query-Logger
Mar 11, 2026 am 11:12 AM
GORM does not print complete SQL by default in order to prevent sensitive data from leaking and reduce log volume. It is necessary to customize the Logger and rewrite the LogMode and Info methods to splice sql.String() and sql.Variables to achieve parameterized output. SlowThreshold only counts the time spent on the GORM layer, and does not include network and database lock waits.
How to implement gRPC server-side streaming mode in Golang. Practical combat of real-time data streaming in Go language
Mar 10, 2026 am 10:21 AM
The server-side stream is a gRPC communication mode with a single request from the client and multiple responses from the server. It is suitable for "single push, multiple receive" scenarios such as real-time push and log pulling. The definition needs to declare rpcGetMetrics(MetricsRequest)returns(streamMetricsResponse) in .proto. The server-side implementation must call stream.Send() multiple times and avoid reusing the same instance. The client must loop Recv() and correctly handle io.EOF.
How to parse and generate CSV files in Golang Go language encoding/csv standard library tips
Mar 10, 2026 am 11:39 AM
csv.Reader defaults to ErrFieldCount instead of panic if the number of fields is inconsistent, but ignoring errors will cause subsequent panic; to tolerate fluctuations, FieldsPerRecord=-1 must be set; csv.Encoder does not handle encoding, and Chinese needs to be manually transcoded or add UTF-8BOM; ReadAll is prone to OOM, and loop Read should be used instead; configurations such as custom delimiters must be set before reading and writing for the first time.
How to implement microservice configuration center hot update in Golang Go language Apollo configuration integration
Mar 10, 2026 am 10:52 AM
ApolloClient.GetConfig() cannot get the updated value because it does not monitor changes by default. You need to explicitly enable long polling (WithLongPolling(true)) and register the AddChangeListener callback. In the callback, deserialize the new configuration and use the atomic pointer to switch instances.
How to use Dapr to build cloud-native microservices in Golang Go language Dapr SDK Development Guide
Mar 10, 2026 am 11:21 AM
The root cause is that the main goroutine is not blocked. dapr.Run() only registers the component and starts the service but does not block the main thread. You need to wait explicitly with select{} or signal.Notify; the business logic should be passed in as a callback or started in an independent goroutine.
How to configure Golang plug-in in VSCode Go language code completion and debugging environment optimization
Mar 10, 2026 am 11:36 AM
Go plug-in installed but not completed? Check whether gopls actually enables VSCode's Go plug-in (golang.go) which relies on gopls to provide semantic completion, jump and diagnosis by default. However, many people think that everything is fine after installing the plug-in - in fact, gopls may not be running at all. Common error phenomena: Ctrl Space only has basic syntax prompts and no field/method completion; F12 jump fails; no govet or staticcheck error is reported after saving. Open the command panel (Ctrl Shift P), run Go:Install/UpdateTools, check gopls and confirm the installation
How to operate Docker containers through API in Golang. Use of Go language Docker Client SDK
Mar 10, 2026 am 11:33 AM
HostConfig and NetworkingConfig need to be initialized explicitly; ContainerConfig.Image must be filled with the tagged image name; HostConfig.Mounts.Type must be lowercase; port mapping requires the cooperation of ExposedPorts and PortBindings; ContainerCreate returns the ID to indicate successful registration and can be started immediately; the log must be set to Follow=false and received with io.Copy; the exitcode should obtain the StatusCode through ContainerWait.





