Repository navigation
docs: add example to count ERROR lines in a log file (#236) #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shreyaabaranwal
wants to merge
3
commits into
bitfield:master
Choose a base branch
from
shreyaabaranwal:add-examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
0aaf8b9
docs: add example to count ERROR lines in a log file (#236)
shreyaabaranwal c30c52d
docs: add example to filter DOWN servers from a CSV (#236)
shreyaabaranwal e432526
docs: simplify examples to open files directly and link from README (…
shreyaabaranwal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
docs: add example to filter DOWN servers from a CSV (#236)
Signed-off-by: shreyaabaranwal <shreyabaranwal229@gmail.com>
- Loading branch information
commit c30c52d253ff4fc3bdd0055087e8496ffb9b3c76
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //go:build ignore | ||
|
|
||
| // This program reads a CSV file containing server names and their status (comma separated), | ||
| // filters only the lines where the status is "DOWN", and prints the name of each such server (first column). | ||
| // It uses the github.com/bitfield/script library to handle the file reading, matching, replacing, and filtering. | ||
| // | ||
| // Equivalent shell command: | ||
| // grep DOWN servers.csv | cut -d, -f1 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/bitfield/script" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Check if examples/servers.csv exists, otherwise fallback to servers.csv. | ||
| csvFile := "examples/servers.csv" | ||
| if _, err := os.Stat(csvFile); os.IsNotExist(err) { | ||
| csvFile = "servers.csv" | ||
| } | ||
|
|
||
| _, err := script.File(csvFile).Match("DOWN").Replace(",", " ").Column(1).Stdout() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error reading CSV file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| server1,UP | ||
| server2,DOWN | ||
| server3,DOWN | ||
| server4,UP | ||
| server5,DOWN |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we actually need this. I mean, it's absolutely correct in the sense that this file isn't part of the
scriptpackage. On the other hand, one thing about example programs is that people will copy and paste them exactly as is—that's what they're for, after all. Including this line would stop their program working, which might be very puzzling for beginners.Does it do us any harm to omit the build tag in examples?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question — I tried it, and removing the build tag does break things: with both count_errors.go and filter_csv.go declaring func main() in the same examples/ directory,
go build ./...andgo vet ./...fail with "main redeclared".go run file.gohandles one file fine, but a directory-wide build compiles them as one package, so the tag was working around exactly that.To keep them copy-paste-able without the tag, my instinct is to give each example its own subdirectory (examples/count_errors/main.go, examples/filter_csv/main.go) — scales cleanly and
go build ./...stays happy. But it's your call on the layout. Which would you prefer?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we're working against the grain of Go a little bit here, aren't we? It's actually quite awkward to include a copypastable program in a Go module that doesn't cause a
mainconflict.Maybe what we should do instead is use Go's built-in example mechanism. For example (sorry), we could add this to
script_test.go:As you probably know, because the function names start with
Example, they'll be built as part of the autogenerated documentation, and because they don't include the names of any other identifier, they'll be treated as package-level examples:We can put the data files in the
testdatafolder—I don't think the publicpkgsiteinstance will let you read these, but that's okay. People can copy and paste the code and it'll work.