[Go]good sample of Go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

From the go language, cloud power

1 Package Main2 3 Import (4     "io"5     "Log"6     "net/http"7     "OS"8     "os/exec"9     "StrConv"Ten ) One  A varUniq = Make (chanint) -  - func init () { the go func () { -          forI: =0; ; i++ { -Uniq <-I -         } +     }() - } +  A Func Main () { at     ifERR: = OS. Chdir (OS. TempDir ()); Err! =Nil { - log. Fatal (ERR) -     } -  -http. Handlefunc ("/", FrontPage) -http. Handlefunc ("/compile", Compile) inLog. Fatal (http. Listenandserve ("127.0.0.1:1234", nil)) - } to  +Func FrontPage (w http. Responsewriter, _ *http. Request) { -W.write ([]byte(frontPage)) the } *  $Func Err (w http. Responsewriter, e error)BOOL {Panax Notoginseng     ifE! =Nil { -W.write ([]byte(E.error ())) the         return true +     } A     return false the } +  -Func Compile (w http. Responsewriter, req *http. Request) { $x: ="Play_"+ StrConv. Itoa (<-uniq) +". Go" $  -F, E: =OS. Create (x) -     ifErr (W, e) { the         return -     }Wuyi  the defer OS. Remove (x) - defer f.close () Wu  -_, E =io. Copy (f, req. Body) About     ifErr (W, e) { $         return -     } - f.close () -  ACMD: = Exec.command ("Go","Run", X) +O, E: =cmd.combinedoutput () the     ifErr (W, e) { -         return $     } the  the w.write (o) the } the  - ConstFrontPage = ' <!doctype html> in the<script> the varreq; About function Compile () { the     varProg = document.getElementById ("Edit"). Value; the     varreq =NewXMLHttpRequest (); theReq.onreadystatechange =function () { +         if(!req | | req.readystate! =4) -             return; thedocument.getElementById ("Output"). InnerHTML =Req.responsetext;Bayi     } theReq.open ("POST","/compile",true); theReq.setrequestheader ("Content-type","Text/plain;charset=utf-8"); - req.send (Prog); - } the  the</script> the the<body> -<textarea rows=" -"cols=" the"Id="Edit"spellcheck="false"> the  the Package Main theImport"FMT"    94 Func Main () { theFmt. Println ("Hello, World") the } the</textarea>98<button onclick="compile ();">run</button> About<div id="Output"></div> -</body>101102`

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.