Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Gore: Go REPL

2.682 Aufrufe

Veröffentlicht am

Go Conference Summer 2015

  • ➤➤ How Long Does She Want You to Last? Here's the link to the FREE report ●●● https://tinyurl.com/rockhardxxx➤➤ 3 Reasons Why You Shouldn't take Pills for ED (important) ♥♥♥ https://tinyurl.com/rockhardxxx
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • DOWNLOAD FULL. BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

Gore: Go REPL

  1. 1. A Tale of Go REPL motemen Go Conference 2015 Summer @ Tokyo
  2. 2. About Me - @motemen - Web engineer - Chief engineer at Hatena, Kyoto
  3. 3. Mackerel mackerel.io
  4. 4. Agenda - Introduction of ! motemen/gore - Gore’s architecture - Implementation details
  5. 5. Gore /gɔː/
  6. 6. Case: Learning New Languages - Playing with language features - Trying libraries
  7. 7. My Case — Perl - perl ~/sketch.pl - Edit and Run
  8. 8. My Case — Ruby - irb - pry - A “REPL”
 (Read-Eval-Print-Loop)
  9. 9. My Case — Go? - tmp.go - “packge main” “func main()” - Removing unused variables by hand - Question: Can gophers haz a REPL?
  10. 10. Go "REPL" impls (AFAIK) - ! sbinet/igo — built upon ! sbinet/go-eval, with liner - ! emicklei/rango — importing, statements, listing variables - ! vito/go-repl — importing, statements, useful commands - ! d4l3k/go-pry — attaches to running program, uses reflection - (play.golang.org)
  11. 11. Needed a REPL that: - Evaluates statements - Imports any module - Pretty prints values automatically - Completes code
  12. 12. The Difficulties - How can we achieve Go semantics? - No APIs provided - Re-implement Go compiler? 😩
  13. 13. Solution: “go run”
  14. 14. Solution: “go run” - An “eval” to Go - Perfect implementation & Always up-to-date - Dead simple - In: a program Out: the result 🎉
  15. 15. Gore: A REPL using “go run” go get github.com/motemen/gore
  16. 16. Caveats 🙏 - Gore runs all code input for each run - Code with side effects will be repeated - eg. Printing output / Sleeping
  17. 17. Stories Inside Gore
  18. 18. Overview: not a “REPL” exactly - REPL: Read-Eval-Print-Loop - No “Eval” and “Print” phase - As we do them by “go run” - Much like: Read-Generate-Run-Loop
  19. 19. Read-Gen-Run-Loop 1. Read user input to evaluate 2. Generate a .go file that prints result 3. “go run” it 4. Back to 1.
  20. 20. Step: Read - Codes go under AST (abstract syntax tree) form - Syntax checking - Easier to manipulate - Input String → AST → .go → “go run”
  21. 21. Read: Input - ! peterh/liner - Line editing - History - Supports Windows
  22. 22. Read: Input to AST - package go/parser - Go has rich support for parsing/typing its code - Easy to generate & manipulate
  23. 23. Read: Input to AST - Parse input as an expression - If failed: a statement - Otherwise: continue input (multi-line)
  24. 24. Read: Expression - Easy - Dedicated API - parser.ParseExpr(in)
  25. 25. Read: Statements - $ func (s *Session) evalStmt(in string) error { src := fmt.Sprintf("package P; func F() { %s }", in) f, err := parser.ParseFile(s.Fset, "stmt.go", src, 0) if err != nil { return err } enclosingFunc := f.Scope.Lookup("F").Decl. (*ast.FuncDecl) stmts := enclosingFunc.Body.List }
  26. 26. Generate: The Delicious Part - Append the input AST nodes to our main() - Add the code to print the value
  27. 27. Printing Values - The generated program prints values (not gore) - “New Values” should be printed - Expression: Its resulting value - Statement: Values assigned
  28. 28. Printing Values: Expression - User: foo.Bar(a+1) - Gore: __gore_p(foo.Bar(a+1))
  29. 29. Printing Values: Statements - ast.AssignStmt stands for assign/define - User: a, b := foo() - Gore: a, b := foo(); __gore_p(a, b)
  30. 30. Printing Values: __gore_p() - “Pretty prints” values - Defined along with the main() - Depends on installed packages - ! k0kubun/pp, ! davecgh/go-spew or plain old %#v
  31. 31. The Initial .go File (pp) package main import "github.com/k0kubun/pp" func __gore_p(xx ...interface{}) { for _, x := range xx { pp.Println(x) } } func main() { // User input goes here }
  32. 32. The Initial .go File (go-spew) package main import "github.com/davecgh/go-spew/spew" func __gore_p(xx ...interface{}) { for _, x := range xx { spew.Printf("%#vn", x) } } func main() { // User input goes here }
  33. 33. The Initial .go File (%#v) package main import "fmt" func __gore_p(xx ...interface{}) { for _, x := range xx { fmt.Printf("%#vn", x) } } func main() { // User input goes here }
  34. 34. Generate: Append to main() - Just append those generated statements
 s.mainBody.List = append(s.mainBody.List, stmts…) - Now we’ve got the working code! - Really? No! %
  35. 35. Go compiler complaints (you know) - “x declared but not used” - “p imported but not used” - “no new variables of left side of :=“ - & Should remove these to a successful go run
  36. 36. Quick Fixing Erroneous Program - “x declared but not used” - “p imported but not used” - “no new variables on left side of :=“ Use it!! Anonymize it!! Make it `=‘ !!
  37. 37. “declared but not used” package P func main() { s := "hello" } package P func main() { s := "hello" _ = s }
  38. 38. ”imported but not used” package P import "fmt" func main() { } package P import _ "fmt" func main() { }
  39. 39. “no new variables on left side of :=“ package P func main() { var a int a := 1 } package P func main() { var a int a = 1 }
  40. 40. ! motemen/go-quickfix to do the work - Type check source code using go/types.Check() - Catch errors and modify AST - Packed with a bin - goquickfix -w main.go
  41. 41. Gore specific quickfix - __gore_p() for non-value expressions
  42. 42. Running - Output the AST to file — go/printer - Then go run - If failed to run, revert the last input
  43. 43. Recap: Read-Gen-Quickfix-Run-Loop 1. Read and parse the input to obtain AST 2. Add printing function call __gore_p() 3. Append the code to main() 4. Quickfix it so that it compiles well 5. go run
  44. 44. PRs welcome! 🍻 - ! motemen/gore - ! motemen/go-quickfix
  45. 45. htn.to/intern2015
  46. 46. A Tale Gore motemen Go Conference 2015 Summer @ Tokyo
  47. 47. Appendix: More Features
  48. 48. Code Completion - liner has support for completion - Great tool for editors: ! nsf/gocode - Server/client model - And an interface to it: motemen/gore/gocode - If gocode binary can be located, use it
  49. 49. Commands - :print - :write <file> - :import <pkg> - :doc <expr>
  50. 50. :import <pkg> - Imports arbitrary packages - Just append import statement to the file - Unused imports are handled by quickfix
  51. 51. :doc <expr> - Invokes go doc for given expression - Package: :doc json - Function: :doc json.Marshal - Method: :doc json.NewEncoder().Encode

×