Git Product home page Git Product logo

golf's Introduction

golf: Go one-liner fun

GoDev

Golf follows the tradition of shell, awk, and perl, letting you write Go programs as one-liners.

Invoke golf with a snippet of Go code in the -e flag, which will be compiled and run for you.

Additional flags such as -n turn on awk/perl-like line mode, which are useful in processing text data. See the examples below and in the godoc.

Some variables and functions are provided in the prelude package. These are inlined and made available to the one-liner. They are common elements of one-liner coding, for example the current line being processed in line mode.

Examples

  # cat -n (see more about "line mode" in command godoc)
  golf -ne 'fmt.Printf("%6d  %s", LineNum, Line)' FILE1 FILE2

  # head MYFILE
  golf -pe 'if LineNum == 10 { break File }' MYFILE

  # Additional packages may be imported using -M pkg. Alternatively,
  # specify the -g flag to let goimports figure it out.
  golf -gle 'Print("The time is ", time.Now())'

  # -a mode (which implies -n) automatically splits input fields.
  # These can be accessed from the Fields slice, or using
  # the convenient Field accessor (supports 1-based and negative indexes).
  ps aux | golf -ale 'Print(Field(5))'
  echo "tom, dick, and harry" | golf -ale 'Print(Field(-2))'

  # Sum sizes. Note flags replacing awk/perl BEGIN and END blocks.
  ls -l | golf -al -BEGIN 'sum := 0' -e 'sum += GAtoi(Field(5))' -END 'Print(sum)'

  # Convert TSV to CSV.
  golf -F '/\t/' -ple 'for i, v := range Fields { Fields[i] = strconv.Quote(v) }; Line = Join(Fields, ",")'

  # Upper-case the contents of files, editing them in-place.
  golf -pI .bak -e 'Line = strings.ToUpper(Line)' FILE1 FILE2

  # -i does the same with no backup.
  # Perl users note: -i does not take arguments; "ne" here mean -n -e.
  find . -name \*.css | xargs golf -ine 'Print(strings.ReplaceAll(Line, "chartreuse", "lime"))'

Install

go install github.com/gaal/golf@latest

License

MIT - See LICENSE file.

golf's People

Contributors

gaal avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.