Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OSSで楽に作るGo言語クライアントツール

 OSSで楽に作るGo言語クライアントツール

Goの入門に最適です

Makoto Tano

January 22, 2016
Tweet

More Decks by Makoto Tano

Other Decks in Programming

Transcript

  1. ίϚϯυϥΠϯπʔϧͰؾʹ͢Δ͜ͱ 1. {command} = ίϚϯυຊମ w w w w w

    w 2. {command} -R = ίϚϯυΦϓγϣϯ w w w w w w w w w 3. {command} args.. = ύϥϝʔλ w w w w w 4. {command} help = ίϚϯυϔϧϓ w w w w w w w ɾɾɾ͍ͬͺ͍΍Δ͜ͱΞϧωɻ
  2. ςϯϓϨʔτͷղઆ
 1. ίϚϯυຊମ app.Commands = []cli.Command{ { Name: "hello", Aliases:

    []string{"h"}, Usage: "show Hello World", Action: func(c *cli.Context) { }, }, } ίϚϯυ໊ γϣʔτΧοτ ίϚϯυϔϧϓ ͜͜ʹίϚϯυ಺༰Λॻ͘
 ʢษڧͷ͠Ͳ͜ΖͰ͢ʂʣ
  3. ςϯϓϨʔτͷղઆ
 2. ίϚϯυΦϓγϣϯ app.Flags = []cli.Flag{ cli.BoolFlag{ Name: "dryrun, d",

    }, } Φϓγϣϯ͸Flagͱݺͼ·͢ ϩϯά/γϣʔτωʔϜ “Value”Λ଍͢ͱσϑΥϧτ஋͕ࢦఆՄೳ
  4. ςϯϓϨʔτͷղઆ
 3. ύϥϝʔλ var paramFirst = "" if len(c.Args()) >

    0 { paramFirst = c.Args()[0] } c.Args() Ͱ഑ྻ͕[]stringͰฦΔ .First(), .Tail() ͳͲͰ΋औಘՄೳ
  5. ςϯϓϨʔτͷղઆ
 4. ίϚϯυϔϧϓ mako ~/dev/gopath1.5.2/src/sample $ go run main.go NAME:

    sample_client - github.com/codegangsta/cli ͷςϯϓϨʔτͰ͢ USAGE: main [global options] command [command options] [arguments...] VERSION: 0.0.1 COMMANDS: hello, h hello world Λදࣔ͠·͢ help, h Shows a list of commands or help for one command GLOBAL OPTIONS: --dryrun, -d --help, -h show help --version, -v print the version ͳΜͱطʹͰ͖ͪΌͬͯ·͢ʂ ΠϠοϑʔʂ
  6. ༻ҙͨ͠gistςϯϓϨʔτͷ࣌఺Ͱ͜͜·Ͱಈ͖·͢ # Setup mako ~/dev/gopath1.5.2/src/sample $ go get github.com/codegangsta/cli #

    Execute
 mako ~/dev/gopath1.5.2/src/sample $ vi main.go # ςϯϓϨΛίϐϖ mako ~/dev/gopath1.5.2/src/sample $ go run main.go —dryrun hello Tom ։࢝ this is dry-run
 Hello world! Tom
 ऴྃ
  7. ͓ΘΓ • GoͷSyntaxษڧ͸ʮγϡοͱ golang ʹೖ໳͢Δ࿩ʯ ͕ݸਓతʹ͸޷͖Ͱ͢ɻ
 http://sixeight.hatenablog.com/entry/2015/03/16/032222 • ਺࣌ؒͱΔ͚ͩͰɺGopherʹͳΕΔνϟϯεʂ ಀ͢

    w w Θ͚ʹ͸͍͔ͳ͍Ͱ͢Ͷʂ
 w w w w w w w w ʢͦ͜·Ͱͷ͜ͱ͔ͬͯ࿩΋͋Γ·͕͢ʣ • GoݴޠΠϕϯτ΍ͬͯΔ͔Β༡ͼʹ͖ͯͶʂ
 http://eure.connpass.com/