docs: Update Golang code snippets for the "Feature" section#36423
Conversation
…o fix typo and act upon the potential error
There was a problem hiding this comment.
Greptile Summary
This PR improves the quality of Go code snippets in PostHog's documentation by fixing compilation issues and adding proper error handling. The changes affect two files that generate code snippets for feature flags and experiments.
In FeatureFlagSnippets.tsx, the Go snippet function (GolangSnippet) now includes the proper posthog. package prefix when initializing FeatureFlagPayload structs, which was previously missing and would cause compilation errors. The code also adds essential error handling with an if err != nil block, following Go best practices. Additionally, the indentation has been adjusted to align with other language examples in the documentation.
Similar improvements were made in ExperimentCodeSnippets.tsx to the GolangSnippet function, ensuring consistency across all Go code examples in PostHog's documentation. These changes address issue #114 in the PostHog Go SDK repository and significantly improve the developer experience by providing copy-paste ready code that actually compiles and follows Go conventions.
The modifications integrate well with the existing codebase structure, which already has established patterns for generating language-specific code snippets through TypeScript functions that return JSX elements containing CodeSnippet components.
Confidence score: 5/5
- This PR is extremely safe to merge with virtually no risk of breaking existing functionality
- Score reflects simple, well-targeted improvements to documentation code snippets with clear benefit and no side effects
- No files require special attention as the changes are straightforward documentation improvements
Context used:
Context - Default templates for code should be clear and useful, providing examples of how to write the code effectively, rather than relying solely on documentation. (link)
2 files reviewed, no comments
|
Not enough context for a review, but your border collie is beautiful. Compliments from a fellow border collie owner 🐕 |
haacked
left a comment
There was a problem hiding this comment.
Changes look good to me. Thanks!
|
@orian these looked good to me but I pinged you as our resident Go expert in case you had some thoughts for further improvements. I'll merge this when everything passes. |
|
Thanks @gewenyu99 😊 ! They're such an amazing breed! Thanks @haacked / @orian for the speedy reviews (also in the other PR), appreciate it! 🙌 |
Problem
Whilst reading the documentation about feature flags and experiments, I noticed that some of the Go snippets were missing the
posthog.import prefix and/or were ignoring error handling. In order to improve the potential first impressions / copyability of the code, I quickly updated them to help out future readers.The indentation adjustments are solely for the sake of aligning with the other code samples, I've included screenshots below to show how it renders, but happy to leave the indentation as is if that is your preferred style 😃 !
I've also noticed it was listed as an issue in the Golang SDK, therefore, this update also closes PostHog/posthog-go#114.
I have also submitted PRs for fixes in the posthog.com repo, and the posthog-go repo so everything is aligned:
Changes
Rendering of the feature-flag sample Go code:

Rendering of the feature-flag sample Go code with local evaluation options enabled:

Rendering of the experiment sample Go code:

How did you test this code?
I ran the PostHog system locally in order to observe how the changes in documentation would render on the pages. Please refer to the screenshots above to see.
👉 Stay up-to-date with PostHog coding conventions for a smoother review.