In the A Tour of Go multilingual translation project, the course content, Playground examples, production deployment, and runtime environment all adopt a design that separates public source code from production configuration.
When the project needed to integrate an advertising system, the initial consideration was to directly add AdSense-related configuration. However, after analysis, I realized that the advertising platform might change in the future, and directly binding AdSense into the code structure would reduce extensibility.
Therefore, this adjustment did not implement AdSense-specific logic. Instead, it abstracted the ad injection capability into a general HTML injection mechanism.
The final implementation:
- Analytics code continues to use independent injection;
- Ad code uses independent injection;
- The production environment provides the actual content via systemd environment variables;
- The public source code only retains the injection position and does not contain any ad account information.
Why not write the ad code directly into the source code
For a public source code project, if you write the ad code directly into the template:
<script async src="https://pagead2.googlesyndication.com/..."></script>Then anyone who pulls the repository and deploys it will use the same ad configuration by default.
This causes:
- Strong coupling between the source code and the production environment;
- Inability for other deployers to configure independently;
- Difficulty in switching ad platforms;
- The public repository containing operational configuration that does not belong to the project itself.
Therefore, the project continues to use the existing analytics code injection method:
Production environment configuration
↓
systemd EnvironmentFile
↓
Go service reads
↓
Template rendering
↓
HTML head injectionFrom AdSense-specific configuration to general ad injection
In the initial design, the ad configuration was easily tied to the AdSense platform.
For example:
TOUR_ADSENSE_CLIENTThis approach can only express:
“Currently using AdSense”.
But in the future, if you need to:
- Change the ad platform;
- Add multiple ad scripts;
- Use other HTML injection content;
All of these would require modifying the code structure again.
Therefore, this refactoring adjusted it to:
TOUR_AD_HTMLThe HTML content to be injected is provided directly by the environment variable.
Corresponding commit:

Go service adds an independent ad injection chain
New injection chain:
TOUR_AD_HTML
↓
os.Getenv("TOUR_AD_HTML")
↓
template.HTML
↓
AdHTML
↓
{{.AnalyticsHTML}}{{.AdHTML}}
↓
HTML <head>In the implementation:
AnalyticsHTMLcontinues to handle the analytics code;AdHTMLhandles the ad HTML;- The two remain independent.
This allows you to control separately:
- Whether to enable analytics;
- Whether to enable ads;
- What ad content to use.

Release package only contains injection capability, not production ad configuration
When the project release process generates the production bundle:
It will not generate:
- AdSense client ID;
- Ad scripts;
- Production environment configuration.
The templates in the release package only retain:
{{.AnalyticsHTML}}{{.AdHTML}}
<meta charset="utf-8">That is:
The source code and release package know “where to inject”.
But they do not know “what to inject”.

Production environment injects ad code via systemd
The actual ad configuration only exists on the production server:
/etc/go-tour/go-tour.envWhere:
TOUR_ANALYTICS='...'
TOUR_AD_HTML='<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=..."></script>'After modifying the production configuration, you only need to restart the service.
The entire process:
/etc/go-tour/go-tour.env
↓
systemd EnvironmentFile
↓
go-tour.service
↓
Read TOUR_AD_HTML
↓
Template rendering
↓
Browser HTML head
Public network verification of ad script output
After deployment, verify via the public web page:
curl -sS https://go-dev.shuijingwanwq.com/ | grep pagead2Returns:
https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.jsThis shows:
The ad injection chain is now in effect.
From production configuration to the final page:
Production environment variables
↓
Go service
↓
HTML rendering
↓
Browser loads ad scriptThis entire process did not modify the ad account configuration in the public source code.

Summary
This ad integration was not simply adding a piece of AdSense script, but further refining the production configuration architecture of the A Tour of Go multilingual translation project.
The final implementation:
- Decoupled ad logic from the specific ad platform;
- Public source code does not contain production ad configuration;
- Different deployment environments can independently set ad content;
- Future support for other ad platforms or custom HTML injection is possible.
Through the environment variable injection mechanism, the project maintains:
Public source code
+
Independent production configuration
+
Extensible deployment methodThis is consistent with the project’s existing analytics code injection, Playground proxy, and production release process, providing a more stable foundation for long-term maintenance.

