gosh -pln '"Hello, World!"'
This prints Hello, World!
gosh -pln 'math.Pi'
This prints the value of Pi
gosh -pln '17*12.5'
This prints the results of a simple calculation
gosh -n -b 'count := 0' -e 'count++' -a-pln 'count'
This reads from the standard input and prints the number of lines read
-n sets up the loop reading from standard input
-b 'count := 0' declares and initialises the counter before the loop
-e 'count++' increments the counter inside the loop
-a-pln 'count' prints the counter using fmt.Println after the loop.
gosh -n -b-p '"Radius: "' -e 'r, err := strconv.ParseFloat(_l.Text(), 64)' -e-s iferr -pf '"Area: %9.2f\n", r*r*math.Pi' -p '"Radius: "'
This repeatedly prompts the user for a Radius and prints the Area of the corresponding circle
-n sets up the loop reading from standard input
-b-p '"Radius: "' prints the first prompt before the loop.
-e 'r, err := strconv.ParseFloat(_l.Text(), 64)' sets the radius from the text read from standard input, ignoring errors.
-e-s iferr checks the error using the 'iferr' snippet
-pf '"Area: %9.2f\n", r*r*math.Pi' calculates and prints the area using fmt.Printf.
-p '"Radius: "' prints the next prompt.
gosh -i -w-pln 'strings.ReplaceAll(string(_l.Text()), "mod/pkg", "mod/v2/pkg")' -- abc.go xyz.go
This changes each line in the two files abc.go and xyz.go replacing any reference to mod/pkg with mod/v2/pkg. You might find this useful when you are upgrading a Go module which has changed its major version number.
The files will be changed and the original contents will be left behind in files called abc.go.orig and xyz.go.orig.
-i sets up the edit-in-place behaviour
-w-pln writes to the new, edited copy of the file
gosh -i -e 'if _fl == 1 {' -w-pln '"// Edited by Gosh!"' -w-pln '' -e '}' -w-pln '_l.Text()' -- abc.go xyz.go
This edits the two files abc.go and xyz.go adding a comment at the top of each file. It finds the top of the file by checking the built-in variable _fl which gives the line number in the current file
The files will be changed and the original contents will be left behind in files called abc.go.orig and xyz.go.orig.
-i sets up the edit-in-place behaviour
-w-pln writes to the new, edited copy of the file
gosh -http-handler 'http.FileServer(http.Dir("/tmp/xxx"))'
This runs a web server that serves files from /tmp/xxx.
gosh -web-p '"Gosh!"'
This runs a web server (listening on port 8080) that returns 'Gosh!' for every request.
gosh -n -e 'if l := len(_l.Text()); l > 80 { ' -pf '"%3d: %s\n", l, _l.Text()' -e '}'
This will read from standard input and print out each line that is longer than 80 characters.
gosh -snippet-list
This will list all the available snippets.
gosh -snippet-list -snippet-list-short -snippet-list-part text -snippet-list-constraint iferr
This will list just the text of the iferr snippet.