Skip to content

docs: add package-level examples (#236) - #254

Draft
PlaidSnowFrog wants to merge 8 commits into
bitfield:masterfrom
PlaidSnowFrog:add-examples
Draft

PlaidSnowFrog wants to merge 8 commits into
bitfield:masterfrom
PlaidSnowFrog:add-examples

Conversation

@PlaidSnowFrog

Copy link
Copy Markdown

Continues #243 (keeping @shreyaabaranwal's commits). Closes #236.

Following the direction agreed in #243:

  • Converted the two original examples (count ERROR lines, filter DOWN servers from a CSV) into Example_ functions in script_test.go, with their data in testdata/.
  • Replaced panic(err) with fmt.Fprintln(os.Stderr, err) followed by return, to match the existing examples in the file. (os.Exit(1) made sense for the standalone programs, but inside an Example_ function it would stop the whole test run.)
  • Added 2 new examples:
    • Example_listExecutables
    • Example_createWriteFile
  • Unix-only examples (like listing executables, which relies on Unix file permissions) are in script_unix_test.go. I'll add a Windows example in script_windows_test.go before marking this ready.

I'd like your opinion on two things:

  1. README links: you suggested the (Listing [name](link)) convention when the examples were separate files. Now that they're Example_ functions, should the README link to their pkg.go.dev anchors instead, or should I leave the README for a separate PR?
  2. Naming: I used camelCase suffixes (Example_listExecutables) to match existing examples like ExampleIfExists_noExec, but your samples used snake_case (Example_count_lines). Happy to switch if you prefer.

go test ./..., go vet ./... and GOOS=windows go vet ./... pass locally.

shreyaabaranwal and others added 8 commits November 18, 2026 18:18
Signed-off-by: shreyaabaranwal <shreyabaranwal229@gmail.com>
Signed-off-by: shreyaabaranwal <shreyabaranwal229@gmail.com>
…itfield#236)

Signed-off-by: shreyaabaranwal <shreyabaranwal229@gmail.com>
Move count_errors and filter_csv into Example_ functions, with their
data files in testdata/, and remove the examples/ directory. Windows compatibility will follow in a later commit.
Replace os.Exit(1) error handling in Example_createWriteFile and other example functions with
return, matching the pattern used by the other examples
(e.g. ExamplePipe_String, ExamplePipe_CountLines).
@bitfield

Copy link
Copy Markdown
Owner

Is this branch up to date with master? It looks like it needs rebasing:

gh pr checkout 254
From github.com:bitfield/script
 ! [rejected]        refs/pull/254/head -> add-examples  (non-fast-forward)
failed to run git: exit status 1

@PlaidSnowFrog

Copy link
Copy Markdown
Author

I think the branch is up to date: it already contains everything on master, and GitHub shows it as able to merge. The rejection might be because you already have a local add-examples branch from #243 (same branch name), which gh is trying to overwrite. I also ran git rebase upstream/master locally and it says the branch is already up to date.

Sorry for the confusion. I'll also push a few fixes soon as I noticed there are still some things that need fixing (a stale README link and some others), plus the Windows example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add examples/ in the repo

3 participants