技术控

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

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

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

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
gorram

   
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

  [code]go get npf.io/gorram[/code]  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:
  [code]$ echo '{ "foo" : "bar" }' | gorram encoding/json Indent "" $'\t'
{
    "foo" : "bar"
}[/code]  Calculate a sha256 sum:
  [code]$ gorram crypto/sha256 Sum256 foo.gz
abcdef012345678[/code]  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:
  [code]usage:
$ cat foo.zip | gorram crypto/sha1 Sum
or
$ gorram crypto/sha1 Sum foo.zip

function:
// crypto/sha1
func Sum(data []byte) [Size]byte[/code]  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] .
  [code]usage:
$ gorram encoding/json Indent foo.json "" $'\t'
or
$ cat foo.json | gorram encoding/json Indent "" $'\t'

function:
// encoding/json
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error[/code]  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.
  [code]usage:
$ gorram math Cos 25

function:
// math
func Cos(x float64) float64[/code]   Gorram understands how to convert CLI arguments using the stringconv.Parse* functions, and will print outputs with fmt.Printf("%v\n", val) .
  [code]usage:
$ echo 12345 | gorram encoding/base64 StdEncoding.EncodeToString
MTIzNDU2Cg==

function:
// base64
func (e *Encoding) EncodeToString(b []byte]) string[/code]  Gorram understands that packages have global variables that have methods you can call.
  Development

   See theproject page for what's being worked on now.
友荐云推荐




上一篇:Fresh EAP with Refactorings to Java 8, Better VCS and Gradle Integrations
下一篇:My Experience Running Development At A Startup
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

筒子们 发表于 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
顶顶更健康
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表