请选择 进入手机版 | 继续访问电脑版

技术控

    今日:0| 主题:63447
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Gorram: go run for any go function

[复制链接]
傷鱂怺恆 发表于 2016-10-7 00:59:31
247 4
gorram

   

Gorram: go run for any go function

Gorram: go run for any go function-1-技术控-interface,something,function,converts,specific

   image: © SubSuid
  It's like go run for any go function.
  Automagically understands how to produce an interface from the command line into a Go function.
   Sometimes, magic is just someone spending more time on something than anyone else might reasonably expect. -Teller
  Installation

  1. go get npf.io/gorram
复制代码
Note: gorram depends on having a working go environment to function, since it dynamically analyzes go code in the stdlib and in your GOPATH.
  Examples

  Pretty print JSON:
  1. $ echo '{ "foo" : "bar" }' | gorram encoding/json Indent "" $'\t'
  2. {
  3.     "foo" : "bar"
  4. }
复制代码
Calculate a sha256 sum:
  1. $ gorram crypto/sha256 Sum256 foo.gz
  2. abcdef012345678
复制代码
How it works

   The first time you run Gorram with a specific function name, Gorram analyzes the package function and generates a file for use with go run . Gorram intelligently converts stdin and/or cli arguments into arguments for the function. Output is converted similarly to stdout. The code is cached in a local directory so that later runs don't incur the generation overhead.
  Heuristics

  By default, Gorram just turns CLI args into function args and prints out the return value of a function using fmt's %v. However, there are some special heuristics that it uses to be smarter about inputs and outputs, based on common go idioms.
  For example:
  1. usage:
  2. $ cat foo.zip | gorram crypto/sha1 Sum
  3. or
  4. $ gorram crypto/sha1 Sum foo.zip
  5. function:
  6. // crypto/sha1
  7. func Sum(data []byte) [Size]byte
复制代码
Gorram understands functions that take a single slice of bytes (or an io.Reader) should read from stdin, or if an argument is specified, the argument is treated as a filename to be read.
   Return values that are an array of bytes are understood to be intended to be printed with fmt's %x, so that you get 2c37424d58 instead of [44 55 66 77 88] .
  1. usage:
  2. $ gorram encoding/json Indent foo.json "" $'\t'
  3. or
  4. $ cat foo.json | gorram encoding/json Indent "" $'\t'
  5. function:
  6. // encoding/json
  7. func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
复制代码
Gorram understands that functions with a src argument that is an io.Reader or []bytes and a dst argument that is a []byte, *bytes.Buffer, or io.Writer will read from stdin (or use an argument as a file to open), and write what is written to dst to stdout.
  Gorram understands that if the function returns a non-nil error, the error should be written to stderr, the program exits with a non-zero exit status, and nothing is written to stdout.
  Gorram understands that prefix and indent are arguments that need to be specified in the command line.
  1. usage:
  2. $ gorram math Cos 25
  3. function:
  4. // math
  5. func Cos(x float64) float64
复制代码
  Gorram understands how to convert CLI arguments using the stringconv.Parse* functions, and will print outputs with fmt.Printf("%v\n", val) .
  1. usage:
  2. $ echo 12345 | gorram encoding/base64 StdEncoding.EncodeToString
  3. MTIzNDU2Cg==
  4. function:
  5. // base64
  6. func (e *Encoding) EncodeToString(b []byte]) string
复制代码
Gorram understands that packages have global variables that have methods you can call.
  Development

   See theproject page for what's being worked on now.
筒子们 发表于 2016-11-9 08:20:44
又见技术帖
回复 支持 反对

使用道具 举报

段浩 发表于 2016-11-16 10:54:38
世界末日我都挺过去了,看到傷鱂怺恆我才知道为什么上帝留我到现在!
回复 支持 反对

使用道具 举报

weimei2011 发表于 2016-11-21 12:31:41
为失联儿童祈福!2016-11-21
回复 支持 反对

使用道具 举报

蹲厕所丶找爱 发表于 2016-11-21 18:27:38
顶顶更健康
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表