index.html 2.1 KB

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