index.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeMirror: Go mode</title>
  6. <link rel="stylesheet" href="../../lib/codemirror.css">
  7. <link rel="stylesheet" href="../../theme/elegant.css">
  8. <script src="../../lib/codemirror.js"></script>
  9. <script src="../../addon/edit/matchbrackets.js"></script>
  10. <script src="go.js"></script>
  11. <link rel="stylesheet" href="../../doc/docs.css">
  12. <style>.CodeMirror {border:1px solid #999; background:#ffc}</style>
  13. </head>
  14. <body>
  15. <h1>CodeMirror: Go mode</h1>
  16. <form><textarea id="code" name="code">
  17. // Prime Sieve in Go.
  18. // Taken from the Go specification.
  19. // Copyright © The Go Authors.
  20. package main
  21. import "fmt"
  22. // Send the sequence 2, 3, 4, ... to channel 'ch'.
  23. func generate(ch chan&lt;- int) {
  24. for i := 2; ; i++ {
  25. ch &lt;- i // Send 'i' to channel 'ch'
  26. }
  27. }
  28. // Copy the values from channel 'src' to channel 'dst',
  29. // removing those divisible by 'prime'.
  30. func filter(src &lt;-chan int, dst chan&lt;- int, prime int) {
  31. for i := range src { // Loop over values received from 'src'.
  32. if i%prime != 0 {
  33. dst &lt;- i // Send 'i' to channel 'dst'.
  34. }
  35. }
  36. }
  37. // The prime sieve: Daisy-chain filter processes together.
  38. func sieve() {
  39. ch := make(chan int) // Create a new channel.
  40. go generate(ch) // Start generate() as a subprocess.
  41. for {
  42. prime := &lt;-ch
  43. fmt.Print(prime, "\n")
  44. ch1 := make(chan int)
  45. go filter(ch, ch1, prime)
  46. ch = ch1
  47. }
  48. }
  49. func main() {
  50. sieve()
  51. }
  52. </textarea></form>
  53. <script>
  54. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  55. theme: "elegant",
  56. matchBrackets: true,
  57. indentUnit: 8,
  58. tabSize: 8,
  59. indentWithTabs: true,
  60. mode: "text/x-go"
  61. });
  62. </script>
  63. <p><strong>MIME type:</strong> <code>text/x-go</code></p>
  64. </body>
  65. </html>