openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

index.html (2174B)


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