[轉]good sample of Go

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

from 《Go語言.雲動力》

 

  1 package main  2   3 import (  4     "io"  5     "log"  6     "net/http"  7     "os"  8     "os/exec"  9     "strconv" 10 ) 11  12 var uniq = make(chan int) 13  14 func init() { 15     go func() { 16         for i := 0; ; i++ { 17             uniq <- i 18         } 19     }() 20 } 21  22 func main() { 23     if err := os.Chdir(os.TempDir()); err != nil { 24         log.Fatal(err) 25     } 26  27     http.HandleFunc("/", FrontPage) 28     http.HandleFunc("/compile", Compile) 29     log.Fatal(http.ListenAndServe("127.0.0.1:1234", nil)) 30 } 31  32 func FrontPage(w http.ResponseWriter, _ *http.Request) { 33     w.Write([]byte(frontPage)) 34 } 35  36 func err(w http.ResponseWriter, e error) bool { 37     if e != nil { 38         w.Write([]byte(e.Error())) 39         return true 40     } 41     return false 42 } 43  44 func Compile(w http.ResponseWriter, req *http.Request) { 45     x := "play_" + strconv.Itoa(<-uniq) + ".go" 46  47     f, e := os.Create(x) 48     if err(w, e) { 49         return 50     } 51  52     defer os.Remove(x) 53     defer f.Close() 54  55     _, e = io.Copy(f, req.Body) 56     if err(w, e) { 57         return 58     } 59     f.Close() 60  61     cmd := exec.Command("go", "run", x) 62     o, e := cmd.CombinedOutput() 63     if err(w, e) { 64         return 65     } 66  67     w.Write(o) 68 } 69  70 const frontPage = `<!doctype html> 71 <html><head> 72 <script> 73 var req; 74 function compile(){ 75     var prog = document.getElementById("edit").value; 76     var req = new XMLHttpRequest (); 77     req.onreadystatechange = function() { 78         if (!req || req.readyState != 4) 79             return; 80         document.getElementById("output").innerHTML = req.responseText; 81     } 82     req.open("POST", "/compile", true); 83     req.setRequestHeader("Content-Type", "text/plain;charset=utf-8"); 84     req.send(prog); 85 } 86  87 </script> 88 </head> 89 <body> 90 <textarea rows="25" cols="80" id="edit" spellcheck="false"> 91  92 package main 93 import "fmt"     94 func main(){ 95     fmt.Println("hello, world") 96 } 97 </textarea> 98 <button onclick="compile();">run</button> 99 <div id="output"></div>100 </body>101 </html>102 `

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.