├── .prettierrc ├── AUTHORS ├── CONTRIBUTING.md ├── CONTRIBUTORS ├── LICENSE ├── PATENTS ├── README.md ├── TRANSLATE.md ├── _content ├── doc │ ├── articles │ │ ├── go_command.html │ │ ├── index.html │ │ ├── race_detector.html │ │ └── wiki │ │ │ ├── edit.html │ │ │ ├── final-noclosure.go │ │ │ ├── final-noerror.go │ │ │ ├── final-parsetemplate.go │ │ │ ├── final-template.go │ │ │ ├── final.go │ │ │ ├── final_test.go │ │ │ ├── http-sample.go │ │ │ ├── index.html │ │ │ ├── notemplate.go │ │ │ ├── part1-noerror.go │ │ │ ├── part1.go │ │ │ ├── part2.go │ │ │ ├── part3-errorhandling.go │ │ │ ├── part3.go │ │ │ ├── test_Test.txt.good │ │ │ ├── test_edit.good │ │ │ ├── test_view.good │ │ │ ├── view.html │ │ │ └── wiki_test.go │ ├── cmd.html │ ├── code.html │ ├── codewalk │ │ ├── codewalk.css │ │ ├── codewalk.js │ │ ├── codewalk.xml │ │ ├── codewalk_test.go │ │ ├── functions.xml │ │ ├── markov.go │ │ ├── markov.xml │ │ ├── pig.go │ │ ├── popout.png │ │ ├── sharemem.xml │ │ └── urlpoll.go │ ├── conduct.html │ ├── contrib.html │ ├── contribute.html │ ├── copyright.html │ ├── debugging_with_gdb.html │ ├── devel │ │ ├── pre_go1.html │ │ ├── release.html │ │ └── weekly.html │ ├── diagnostics.html │ ├── docs.html │ ├── download.js │ ├── editors.html │ ├── effective_go.html │ ├── gccgo_contribute.html │ ├── gccgo_install.html │ ├── go-get-install-deprecation.md │ ├── go-logo-black.png │ ├── go-logo-blue.png │ ├── go-logo-white.png │ ├── go1.1.html │ ├── go1.10.html │ ├── go1.11.html │ ├── go1.12.html │ ├── go1.13.html │ ├── go1.14.html │ ├── go1.15.html │ ├── go1.16.html │ ├── go1.2.html │ ├── go1.3.html │ ├── go1.4.html │ ├── go1.5.html │ ├── go1.6.html │ ├── go1.7.html │ ├── go1.8.html │ ├── go1.9.html │ ├── go1.html │ ├── go1compat.html │ ├── go_faq.html │ ├── gopath_code.html │ ├── gopher │ │ ├── README │ │ ├── appenginegopher.jpg │ │ ├── appenginegophercolor.jpg │ │ ├── appenginelogo.gif │ │ ├── biplane.jpg │ │ ├── bumper.png │ │ ├── bumper192x108.png │ │ ├── bumper320x180.png │ │ ├── bumper480x270.png │ │ ├── bumper640x360.png │ │ ├── doc.png │ │ ├── favicon.svg │ │ ├── fiveyears.jpg │ │ ├── frontpage.png │ │ ├── gopherbw.png │ │ ├── gophercolor.png │ │ ├── gophercolor16x16.png │ │ ├── help.png │ │ ├── modelsheet.jpg │ │ ├── pencil │ │ │ ├── gopherhat.jpg │ │ │ ├── gopherhelmet.jpg │ │ │ ├── gophermega.jpg │ │ │ ├── gopherrunning.jpg │ │ │ ├── gopherswim.jpg │ │ │ └── gopherswrench.jpg │ │ ├── pkg.png │ │ ├── project.png │ │ ├── ref.png │ │ ├── run.png │ │ └── talks.png │ ├── hats.js │ ├── help.html │ ├── ie.css │ ├── install-source.html │ ├── install.html │ ├── manage-install.html │ ├── mod.md │ ├── modules │ │ ├── developing.md │ │ ├── gomod-ref.md │ │ ├── images │ │ │ ├── multiple-modules.png │ │ │ ├── single-module.png │ │ │ ├── source-hierarchy.png │ │ │ ├── v2-branch-module.png │ │ │ ├── v2-module.png │ │ │ └── version-number.png │ │ ├── major-version.md │ │ ├── managing-dependencies.md │ │ ├── managing-source.md │ │ ├── publishing.md │ │ ├── release-workflow.md │ │ └── version-numbers.md │ ├── mvs │ │ ├── buildlist.svg │ │ ├── downgrade.svg │ │ ├── exclude.svg │ │ ├── get-downgrade.svg │ │ ├── get-upgrade.svg │ │ ├── replace.svg │ │ └── upgrade.svg │ ├── play │ │ ├── fib.go │ │ ├── hello.go │ │ ├── life.go │ │ ├── peano.go │ │ ├── pi.go │ │ ├── sieve.go │ │ ├── solitaire.go │ │ └── tree.go │ ├── progs │ │ ├── cgo1.go │ │ ├── cgo2.go │ │ ├── cgo3.go │ │ ├── cgo4.go │ │ ├── defer.go │ │ ├── defer2.go │ │ ├── eff_bytesize.go │ │ ├── eff_qr.go │ │ ├── eff_sequence.go │ │ ├── eff_unused1.go │ │ ├── eff_unused2.go │ │ ├── error.go │ │ ├── error2.go │ │ ├── error3.go │ │ ├── error4.go │ │ ├── go1.go │ │ ├── gobs1.go │ │ ├── gobs2.go │ │ ├── image_draw.go │ │ ├── image_package1.go │ │ ├── image_package2.go │ │ ├── image_package3.go │ │ ├── image_package4.go │ │ ├── image_package5.go │ │ ├── image_package6.go │ │ ├── interface.go │ │ ├── interface2.go │ │ ├── json1.go │ │ ├── json2.go │ │ ├── json3.go │ │ ├── json4.go │ │ ├── json5.go │ │ ├── run.go │ │ ├── slices.go │ │ ├── timeout1.go │ │ └── timeout2.go │ ├── root.html │ ├── security.html │ ├── share.png │ ├── tos.html │ └── tutorial │ │ ├── add-a-test.html │ │ ├── call-module-code.html │ │ ├── compile-install.html │ │ ├── create-module.html │ │ ├── getting-started.html │ │ ├── greetings-multiple-people.html │ │ ├── handle-errors.html │ │ ├── images │ │ ├── function-syntax.graffle │ │ └── function-syntax.png │ │ ├── index.html │ │ ├── module-conclusion.html │ │ └── random-greeting.html ├── favicon.ico ├── lib │ └── godoc │ │ ├── analysis │ │ ├── call-eg.png │ │ ├── call3.png │ │ ├── callers1.png │ │ ├── callers2.png │ │ ├── chan1.png │ │ ├── chan2a.png │ │ ├── chan2b.png │ │ ├── error1.png │ │ ├── help.html │ │ ├── ident-def.png │ │ ├── ident-field.png │ │ ├── ident-func.png │ │ ├── ipcg-func.png │ │ ├── ipcg-pkg.png │ │ ├── typeinfo-pkg.png │ │ └── typeinfo-src.png │ │ ├── codewalk.html │ │ ├── codewalkdir.html │ │ ├── dirlist.html │ │ ├── error.html │ │ ├── example.html │ │ ├── godoc.html │ │ ├── godocs.js │ │ ├── images │ │ ├── cloud-download.svg │ │ ├── footer-gopher.jpg │ │ ├── go-logo-blue.svg │ │ ├── home-gopher.png │ │ ├── minus.gif │ │ ├── play-link.svg │ │ ├── plus.gif │ │ ├── treeview-black-line.gif │ │ ├── treeview-black.gif │ │ ├── treeview-default-line.gif │ │ ├── treeview-default.gif │ │ ├── treeview-gray-line.gif │ │ └── treeview-gray.gif │ │ ├── jquery.js │ │ ├── package.html │ │ ├── packageroot.html │ │ ├── play.js │ │ ├── playground.js │ │ └── style.css └── robots.txt ├── cmd ├── admingolangorg │ ├── .gcloudignore │ ├── README.md │ ├── app.yaml │ └── main.go ├── golangorg │ ├── Dockerfile.prod │ ├── Makefile │ ├── README.md │ ├── app.dev.yaml │ ├── app.prod.yaml │ ├── cloudbuild.yaml │ ├── codewalk.go │ ├── go115.go │ ├── godoc.go │ ├── godoc_test.go │ ├── handlers.go │ ├── hg-git-mapping.bin │ ├── local.go │ ├── main.go │ ├── prod.go │ ├── regtest_test.go │ ├── release_test.go │ ├── x.go │ └── x_test.go └── googlegolangorg │ ├── .gcloudignore │ ├── README.md │ ├── app.yaml │ └── main.go ├── codereview.cfg ├── content.go ├── go.mod ├── go.sum └── internal ├── api ├── api.go └── api_test.go ├── dl ├── dl.go ├── dl_test.go ├── server.go ├── server_test.go └── tmpl.go ├── env └── env.go ├── godoc ├── astfuncs.go ├── corpus.go ├── examplefuncs.go ├── godoc.go ├── godoc_test.go ├── markdown.go ├── meta.go ├── page.go ├── pres.go ├── server.go ├── server_test.go ├── tab.go ├── template.go ├── util.go └── versions.go ├── history ├── history.go ├── release.go └── release_test.go ├── memcache ├── memcache.go └── memcache_test.go ├── pkgdoc ├── dir.go ├── dir_test.go ├── doc.go └── doc_test.go ├── proxy └── proxy.go ├── redirect ├── hash.go ├── redirect.go ├── redirect_test.go └── rietveld.go ├── short ├── short.go ├── short_test.go └── tmpl.go ├── spec ├── spec.go └── spec_test.go └── texthtml ├── ast.go └── texthtml.go /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "es5" 4 | } -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # This source code refers to The Go Authors for copyright purposes. 2 | # The master list of authors is in the main Go distribution, 3 | # visible at https://tip.golang.org/AUTHORS. 4 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Go 2 | 3 | Go is an open source project. 4 | 5 | It is the work of hundreds of contributors. We appreciate your help! 6 | 7 | ## Filing issues 8 | 9 | When [filing an issue](https://golang.org/issue/new), make sure to answer these five questions: 10 | 11 | 1. What version of Go are you using (`go version`)? 12 | 2. What operating system and processor architecture are you using? 13 | 3. What did you do? 14 | 4. What did you expect to see? 15 | 5. What did you see instead? 16 | 17 | General questions should go to the [golang-nuts mailing list](https://groups.google.com/group/golang-nuts) instead of the issue tracker. 18 | The gophers there will answer or ask you to file an issue if you've tripped over a bug. 19 | 20 | ## Contributing code 21 | 22 | Please read the [Contribution Guidelines](https://golang.org/doc/contribute.html) 23 | before sending patches. 24 | 25 | Unless otherwise noted, the Go source files are distributed under 26 | the BSD-style license found in the LICENSE file. 27 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | # This source code was written by the Go contributors. 2 | # The master list of contributors is in the main Go distribution, 3 | # visible at https://tip.golang.org/CONTRIBUTORS. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 The Go Authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of Google Inc. nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /PATENTS: -------------------------------------------------------------------------------- 1 | Additional IP Rights Grant (Patents) 2 | 3 | "This implementation" means the copyrightable works distributed by 4 | Google as part of the Go project. 5 | 6 | Google hereby grants to You a perpetual, worldwide, non-exclusive, 7 | no-charge, royalty-free, irrevocable (except as stated in this section) 8 | patent license to make, have made, use, offer to sell, sell, import, 9 | transfer and otherwise run, modify and propagate the contents of this 10 | implementation of Go, where such license applies only to those patent 11 | claims, both currently owned or controlled by Google and acquired in 12 | the future, licensable by Google that are necessarily infringed by this 13 | implementation of Go. This grant does not include claims that would be 14 | infringed only as a consequence of further modification of this 15 | implementation. If you or your agent or exclusive licensee institute or 16 | order or agree to the institution of patent litigation against any 17 | entity (including a cross-claim or counterclaim in a lawsuit) alleging 18 | that this implementation of Go or any code incorporated within this 19 | implementation of Go constitutes direct or contributory patent 20 | infringement, or inducement of patent infringement, then any patent 21 | rights granted to you under this License for this implementation of Go 22 | shall terminate as of the date such litigation is filed. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go website 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/golang.org/x/website.svg)](https://pkg.go.dev/golang.org/x/website) 4 | 5 | This repo holds content and serving programs for the golang.org web site. 6 | 7 | Content is in _content/. Server code is in cmd/ and internal/. 8 | 9 | To run the server to preview local content changes, use: 10 | 11 | go run ./cmd/golangorg 12 | 13 | The supporting programs cmd/admingolangorg and cmd/googlegolangorg 14 | are the servers for admin.golang.org and google.golang.org. 15 | (They do not use the _content/ directory.) 16 | 17 | Each command directory has its own README.md explaining deployment. 18 | 19 | ## JS/CSS Formatting 20 | 21 | This repository uses [prettier](https://prettier.io/) to format JS and CSS files. 22 | 23 | The version of `prettier` used is 1.18.2. 24 | 25 | It is encouraged that all JS and CSS code be run through this before submitting 26 | a change. However, it is not a strict requirement enforced by CI. 27 | 28 | ## Report Issues / Send Patches 29 | 30 | This repository uses Gerrit for code changes. To learn how to submit changes to 31 | this repository, see https://golang.org/doc/contribute.html. 32 | 33 | The main issue tracker for the website repository is located at 34 | https://github.com/golang/go/issues. Prefix your issue with "x/website:" in the 35 | subject line, so it is easy to find. 36 | 37 | -------------------------------------------------------------------------------- /TRANSLATE.md: -------------------------------------------------------------------------------- 1 | # 翻译流程 2 | ## 翻译内容 3 | 1. `studygolang/website`下 `/content/static`中的 `*.html`。 4 | 2. `studygolang/go`下 `src` 目录中的标准库 `*.go`。 5 | 3. `studygolang/go`下 `doc`目录中 `*.html` 。 6 | ## 基础工作 7 | 1. fork [`https://github.com/studygolang/go`](https://github.com/studygolang/go)的代码到自己的仓库。 8 | 2. clone go项目 到本地之后,切换到`release-branch.go1.15` 分支 9 | 3. clone 本项目(website) 到本地 10 | ```bash 11 | git clone https://github.com.cnpmjs.org/studygolang/website.git 12 | git clone https://github.com.cnpmjs.org/your-name/go.git 13 | cd go 14 | git checkout release-branch.go1.15 15 | ``` 16 | 17 | ## 提翻译issue 18 | 1. 在上面的翻译内容列表里找想要翻译的模块或者文件。 19 | 同时去issue中查看是否有人已经在翻译了,如果没有就可以翻译。 20 | 2. 在本项目中(website) 发起一个issue,名字类似于:`翻译:标准库image`等表明自己需要翻译的文档。 21 | 22 | ## 开始翻译 23 | 1. 在 go `release-branch.go1.15`或者 website 的 `master`分支 的基础上 检出一个分支,在该分支下做文档翻译 `git checkout -b your-branch`。 24 | 2. 翻译规范请参照[`https://github.com/studygolang/GCTT/blob/master/chinese-copywriting-guidlines.md`](https://github.com/studygolang/GCTT/blob/master/chinese-copywriting-guidlines.md) 25 | 2. 可以在 webiste目录下,启动本地server,在`localhost:6060`下查看自己的文档翻译效果 `go run ./cmd/golangorg -goroot=../go` 26 | 3. push到自己的远端 27 | 28 | ## 提pr 29 | 1. 在`studygolang/go` 或 `studygolang/website` 项目中,发起pull request,等待合入。 30 | 2. 在`pull request` 中加入 `close close studygolang/website#your-{issueId}`可在pr合入时自动关闭issue。 31 | 32 | ## 建议 33 | 1. 最好一个模块单独一个issue和pr 34 | 2. 建议先启动 server 看看需要改动哪些,`_test.go`,未导出变量等等均可不用翻译。 35 | 3. 如果发现还有需要改动的地方,也可以先转为 draft ,等修改完成之后再合入。 36 | 3. 提 pr 之后如果有问题,可以直接在原pr上改,最好别重新提 pr。 37 | -------------------------------------------------------------------------------- /_content/doc/articles/index.html: -------------------------------------------------------------------------------- 1 | 4 | 5 |

6 | See the Documents page and the 7 | Blog index for a complete list of Go articles. 8 |

9 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/edit.html: -------------------------------------------------------------------------------- 1 |

Editing {{.Title}}

2 | 3 |
4 |
5 |
6 |
7 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/final-noclosure.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "errors" 11 | "html/template" 12 | "io/ioutil" 13 | "log" 14 | "net/http" 15 | "regexp" 16 | ) 17 | 18 | type Page struct { 19 | Title string 20 | Body []byte 21 | } 22 | 23 | func (p *Page) save() error { 24 | filename := p.Title + ".txt" 25 | return ioutil.WriteFile(filename, p.Body, 0600) 26 | } 27 | 28 | func loadPage(title string) (*Page, error) { 29 | filename := title + ".txt" 30 | body, err := ioutil.ReadFile(filename) 31 | if err != nil { 32 | return nil, err 33 | } 34 | return &Page{Title: title, Body: body}, nil 35 | } 36 | 37 | func viewHandler(w http.ResponseWriter, r *http.Request) { 38 | title, err := getTitle(w, r) 39 | if err != nil { 40 | return 41 | } 42 | p, err := loadPage(title) 43 | if err != nil { 44 | http.Redirect(w, r, "/edit/"+title, http.StatusFound) 45 | return 46 | } 47 | renderTemplate(w, "view", p) 48 | } 49 | 50 | func editHandler(w http.ResponseWriter, r *http.Request) { 51 | title, err := getTitle(w, r) 52 | if err != nil { 53 | return 54 | } 55 | p, err := loadPage(title) 56 | if err != nil { 57 | p = &Page{Title: title} 58 | } 59 | renderTemplate(w, "edit", p) 60 | } 61 | 62 | func saveHandler(w http.ResponseWriter, r *http.Request) { 63 | title, err := getTitle(w, r) 64 | if err != nil { 65 | return 66 | } 67 | body := r.FormValue("body") 68 | p := &Page{Title: title, Body: []byte(body)} 69 | err = p.save() 70 | if err != nil { 71 | http.Error(w, err.Error(), http.StatusInternalServerError) 72 | return 73 | } 74 | http.Redirect(w, r, "/view/"+title, http.StatusFound) 75 | } 76 | 77 | func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { 78 | t, err := template.ParseFiles(tmpl + ".html") 79 | if err != nil { 80 | http.Error(w, err.Error(), http.StatusInternalServerError) 81 | return 82 | } 83 | err = t.Execute(w, p) 84 | if err != nil { 85 | http.Error(w, err.Error(), http.StatusInternalServerError) 86 | } 87 | } 88 | 89 | var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$") 90 | 91 | func getTitle(w http.ResponseWriter, r *http.Request) (string, error) { 92 | m := validPath.FindStringSubmatch(r.URL.Path) 93 | if m == nil { 94 | http.NotFound(w, r) 95 | return "", errors.New("invalid Page Title") 96 | } 97 | return m[2], nil // The title is the second subexpression. 98 | } 99 | 100 | func main() { 101 | http.HandleFunc("/view/", viewHandler) 102 | http.HandleFunc("/edit/", editHandler) 103 | http.HandleFunc("/save/", saveHandler) 104 | log.Fatal(http.ListenAndServe(":8080", nil)) 105 | } 106 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/final-noerror.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "html/template" 11 | "io/ioutil" 12 | "log" 13 | "net/http" 14 | ) 15 | 16 | type Page struct { 17 | Title string 18 | Body []byte 19 | } 20 | 21 | func (p *Page) save() error { 22 | filename := p.Title + ".txt" 23 | return ioutil.WriteFile(filename, p.Body, 0600) 24 | } 25 | 26 | func loadPage(title string) (*Page, error) { 27 | filename := title + ".txt" 28 | body, err := ioutil.ReadFile(filename) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &Page{Title: title, Body: body}, nil 33 | } 34 | 35 | func editHandler(w http.ResponseWriter, r *http.Request) { 36 | title := r.URL.Path[len("/edit/"):] 37 | p, err := loadPage(title) 38 | if err != nil { 39 | p = &Page{Title: title} 40 | } 41 | t, _ := template.ParseFiles("edit.html") 42 | t.Execute(w, p) 43 | } 44 | 45 | func viewHandler(w http.ResponseWriter, r *http.Request) { 46 | title := r.URL.Path[len("/view/"):] 47 | p, _ := loadPage(title) 48 | t, _ := template.ParseFiles("view.html") 49 | t.Execute(w, p) 50 | } 51 | 52 | func main() { 53 | http.HandleFunc("/view/", viewHandler) 54 | http.HandleFunc("/edit/", editHandler) 55 | log.Fatal(http.ListenAndServe(":8080", nil)) 56 | } 57 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/final-parsetemplate.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "html/template" 11 | "io/ioutil" 12 | "log" 13 | "net/http" 14 | "regexp" 15 | ) 16 | 17 | type Page struct { 18 | Title string 19 | Body []byte 20 | } 21 | 22 | func (p *Page) save() error { 23 | filename := p.Title + ".txt" 24 | return ioutil.WriteFile(filename, p.Body, 0600) 25 | } 26 | 27 | func loadPage(title string) (*Page, error) { 28 | filename := title + ".txt" 29 | body, err := ioutil.ReadFile(filename) 30 | if err != nil { 31 | return nil, err 32 | } 33 | return &Page{Title: title, Body: body}, nil 34 | } 35 | 36 | func viewHandler(w http.ResponseWriter, r *http.Request, title string) { 37 | p, err := loadPage(title) 38 | if err != nil { 39 | http.Redirect(w, r, "/edit/"+title, http.StatusFound) 40 | return 41 | } 42 | renderTemplate(w, "view", p) 43 | } 44 | 45 | func editHandler(w http.ResponseWriter, r *http.Request, title string) { 46 | p, err := loadPage(title) 47 | if err != nil { 48 | p = &Page{Title: title} 49 | } 50 | renderTemplate(w, "edit", p) 51 | } 52 | 53 | func saveHandler(w http.ResponseWriter, r *http.Request, title string) { 54 | body := r.FormValue("body") 55 | p := &Page{Title: title, Body: []byte(body)} 56 | err := p.save() 57 | if err != nil { 58 | http.Error(w, err.Error(), http.StatusInternalServerError) 59 | return 60 | } 61 | http.Redirect(w, r, "/view/"+title, http.StatusFound) 62 | } 63 | 64 | func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { 65 | t, err := template.ParseFiles(tmpl + ".html") 66 | if err != nil { 67 | http.Error(w, err.Error(), http.StatusInternalServerError) 68 | return 69 | } 70 | err = t.Execute(w, p) 71 | if err != nil { 72 | http.Error(w, err.Error(), http.StatusInternalServerError) 73 | } 74 | } 75 | 76 | var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$") 77 | 78 | func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { 79 | return func(w http.ResponseWriter, r *http.Request) { 80 | m := validPath.FindStringSubmatch(r.URL.Path) 81 | if m == nil { 82 | http.NotFound(w, r) 83 | return 84 | } 85 | fn(w, r, m[2]) 86 | } 87 | } 88 | 89 | func main() { 90 | http.HandleFunc("/view/", makeHandler(viewHandler)) 91 | http.HandleFunc("/edit/", makeHandler(editHandler)) 92 | http.HandleFunc("/save/", makeHandler(saveHandler)) 93 | log.Fatal(http.ListenAndServe(":8080", nil)) 94 | } 95 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/final-template.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "html/template" 11 | "io/ioutil" 12 | "log" 13 | "net/http" 14 | ) 15 | 16 | type Page struct { 17 | Title string 18 | Body []byte 19 | } 20 | 21 | func (p *Page) save() error { 22 | filename := p.Title + ".txt" 23 | return ioutil.WriteFile(filename, p.Body, 0600) 24 | } 25 | 26 | func loadPage(title string) (*Page, error) { 27 | filename := title + ".txt" 28 | body, err := ioutil.ReadFile(filename) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &Page{Title: title, Body: body}, nil 33 | } 34 | 35 | func editHandler(w http.ResponseWriter, r *http.Request) { 36 | title := r.URL.Path[len("/edit/"):] 37 | p, err := loadPage(title) 38 | if err != nil { 39 | p = &Page{Title: title} 40 | } 41 | renderTemplate(w, "edit", p) 42 | } 43 | 44 | func viewHandler(w http.ResponseWriter, r *http.Request) { 45 | title := r.URL.Path[len("/view/"):] 46 | p, _ := loadPage(title) 47 | renderTemplate(w, "view", p) 48 | } 49 | 50 | func saveHandler(w http.ResponseWriter, r *http.Request) { 51 | title := r.URL.Path[len("/save/"):] 52 | body := r.FormValue("body") 53 | p := &Page{Title: title, Body: []byte(body)} 54 | p.save() 55 | http.Redirect(w, r, "/view/"+title, http.StatusFound) 56 | } 57 | 58 | func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { 59 | t, _ := template.ParseFiles(tmpl + ".html") 60 | t.Execute(w, p) 61 | } 62 | 63 | func main() { 64 | http.HandleFunc("/view/", viewHandler) 65 | http.HandleFunc("/edit/", editHandler) 66 | http.HandleFunc("/save/", saveHandler) 67 | log.Fatal(http.ListenAndServe(":8080", nil)) 68 | } 69 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/final.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "html/template" 11 | "io/ioutil" 12 | "log" 13 | "net/http" 14 | "regexp" 15 | ) 16 | 17 | type Page struct { 18 | Title string 19 | Body []byte 20 | } 21 | 22 | func (p *Page) save() error { 23 | filename := p.Title + ".txt" 24 | return ioutil.WriteFile(filename, p.Body, 0600) 25 | } 26 | 27 | func loadPage(title string) (*Page, error) { 28 | filename := title + ".txt" 29 | body, err := ioutil.ReadFile(filename) 30 | if err != nil { 31 | return nil, err 32 | } 33 | return &Page{Title: title, Body: body}, nil 34 | } 35 | 36 | func viewHandler(w http.ResponseWriter, r *http.Request, title string) { 37 | p, err := loadPage(title) 38 | if err != nil { 39 | http.Redirect(w, r, "/edit/"+title, http.StatusFound) 40 | return 41 | } 42 | renderTemplate(w, "view", p) 43 | } 44 | 45 | func editHandler(w http.ResponseWriter, r *http.Request, title string) { 46 | p, err := loadPage(title) 47 | if err != nil { 48 | p = &Page{Title: title} 49 | } 50 | renderTemplate(w, "edit", p) 51 | } 52 | 53 | func saveHandler(w http.ResponseWriter, r *http.Request, title string) { 54 | body := r.FormValue("body") 55 | p := &Page{Title: title, Body: []byte(body)} 56 | err := p.save() 57 | if err != nil { 58 | http.Error(w, err.Error(), http.StatusInternalServerError) 59 | return 60 | } 61 | http.Redirect(w, r, "/view/"+title, http.StatusFound) 62 | } 63 | 64 | var templates = template.Must(template.ParseFiles("edit.html", "view.html")) 65 | 66 | func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { 67 | err := templates.ExecuteTemplate(w, tmpl+".html", p) 68 | if err != nil { 69 | http.Error(w, err.Error(), http.StatusInternalServerError) 70 | } 71 | } 72 | 73 | var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$") 74 | 75 | func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { 76 | return func(w http.ResponseWriter, r *http.Request) { 77 | m := validPath.FindStringSubmatch(r.URL.Path) 78 | if m == nil { 79 | http.NotFound(w, r) 80 | return 81 | } 82 | fn(w, r, m[2]) 83 | } 84 | } 85 | 86 | func main() { 87 | http.HandleFunc("/view/", makeHandler(viewHandler)) 88 | http.HandleFunc("/edit/", makeHandler(editHandler)) 89 | http.HandleFunc("/save/", makeHandler(saveHandler)) 90 | 91 | log.Fatal(http.ListenAndServe(":8080", nil)) 92 | } 93 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/final_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "fmt" 11 | "log" 12 | "net" 13 | "net/http" 14 | ) 15 | 16 | func serve() error { 17 | l, err := net.Listen("tcp", "127.0.0.1:0") 18 | if err != nil { 19 | log.Fatal(err) 20 | } 21 | fmt.Println(l.Addr().String()) 22 | s := &http.Server{} 23 | return s.Serve(l) 24 | } 25 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/http-sample.go: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "log" 8 | "net/http" 9 | ) 10 | 11 | func handler(w http.ResponseWriter, r *http.Request) { 12 | fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) 13 | } 14 | 15 | func main() { 16 | http.HandleFunc("/", handler) 17 | log.Fatal(http.ListenAndServe(":8080", nil)) 18 | } 19 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/notemplate.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "fmt" 11 | "io/ioutil" 12 | "log" 13 | "net/http" 14 | ) 15 | 16 | type Page struct { 17 | Title string 18 | Body []byte 19 | } 20 | 21 | func (p *Page) save() error { 22 | filename := p.Title + ".txt" 23 | return ioutil.WriteFile(filename, p.Body, 0600) 24 | } 25 | 26 | func loadPage(title string) (*Page, error) { 27 | filename := title + ".txt" 28 | body, err := ioutil.ReadFile(filename) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &Page{Title: title, Body: body}, nil 33 | } 34 | 35 | func viewHandler(w http.ResponseWriter, r *http.Request) { 36 | title := r.URL.Path[len("/view/"):] 37 | p, _ := loadPage(title) 38 | fmt.Fprintf(w, "

%s

%s
", p.Title, p.Body) 39 | } 40 | 41 | func editHandler(w http.ResponseWriter, r *http.Request) { 42 | title := r.URL.Path[len("/edit/"):] 43 | p, err := loadPage(title) 44 | if err != nil { 45 | p = &Page{Title: title} 46 | } 47 | fmt.Fprintf(w, "

Editing %s

"+ 48 | "
"+ 49 | "
"+ 50 | ""+ 51 | "
", 52 | p.Title, p.Title, p.Body) 53 | } 54 | 55 | func main() { 56 | http.HandleFunc("/view/", viewHandler) 57 | http.HandleFunc("/edit/", editHandler) 58 | log.Fatal(http.ListenAndServe(":8080", nil)) 59 | } 60 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/part1-noerror.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "fmt" 11 | "io/ioutil" 12 | ) 13 | 14 | type Page struct { 15 | Title string 16 | Body []byte 17 | } 18 | 19 | func (p *Page) save() error { 20 | filename := p.Title + ".txt" 21 | return ioutil.WriteFile(filename, p.Body, 0600) 22 | } 23 | 24 | func loadPage(title string) *Page { 25 | filename := title + ".txt" 26 | body, _ := ioutil.ReadFile(filename) 27 | return &Page{Title: title, Body: body} 28 | } 29 | 30 | func main() { 31 | p1 := &Page{Title: "TestPage", Body: []byte("This is a sample page.")} 32 | p1.save() 33 | p2 := loadPage("TestPage") 34 | fmt.Println(string(p2.Body)) 35 | } 36 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/part1.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "fmt" 11 | "io/ioutil" 12 | ) 13 | 14 | type Page struct { 15 | Title string 16 | Body []byte 17 | } 18 | 19 | func (p *Page) save() error { 20 | filename := p.Title + ".txt" 21 | return ioutil.WriteFile(filename, p.Body, 0600) 22 | } 23 | 24 | func loadPage(title string) (*Page, error) { 25 | filename := title + ".txt" 26 | body, err := ioutil.ReadFile(filename) 27 | if err != nil { 28 | return nil, err 29 | } 30 | return &Page{Title: title, Body: body}, nil 31 | } 32 | 33 | func main() { 34 | p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")} 35 | p1.save() 36 | p2, _ := loadPage("TestPage") 37 | fmt.Println(string(p2.Body)) 38 | } 39 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/part2.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "fmt" 11 | "io/ioutil" 12 | "log" 13 | "net/http" 14 | ) 15 | 16 | type Page struct { 17 | Title string 18 | Body []byte 19 | } 20 | 21 | func (p *Page) save() error { 22 | filename := p.Title + ".txt" 23 | return ioutil.WriteFile(filename, p.Body, 0600) 24 | } 25 | 26 | func loadPage(title string) (*Page, error) { 27 | filename := title + ".txt" 28 | body, err := ioutil.ReadFile(filename) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &Page{Title: title, Body: body}, nil 33 | } 34 | 35 | func viewHandler(w http.ResponseWriter, r *http.Request) { 36 | title := r.URL.Path[len("/view/"):] 37 | p, _ := loadPage(title) 38 | fmt.Fprintf(w, "

%s

%s
", p.Title, p.Body) 39 | } 40 | 41 | func main() { 42 | http.HandleFunc("/view/", viewHandler) 43 | log.Fatal(http.ListenAndServe(":8080", nil)) 44 | } 45 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/part3-errorhandling.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "html/template" 11 | "io/ioutil" 12 | "log" 13 | "net/http" 14 | ) 15 | 16 | type Page struct { 17 | Title string 18 | Body []byte 19 | } 20 | 21 | func (p *Page) save() error { 22 | filename := p.Title + ".txt" 23 | return ioutil.WriteFile(filename, p.Body, 0600) 24 | } 25 | 26 | func loadPage(title string) (*Page, error) { 27 | filename := title + ".txt" 28 | body, err := ioutil.ReadFile(filename) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &Page{Title: title, Body: body}, nil 33 | } 34 | 35 | func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { 36 | t, _ := template.ParseFiles(tmpl + ".html") 37 | t.Execute(w, p) 38 | } 39 | 40 | func viewHandler(w http.ResponseWriter, r *http.Request) { 41 | title := r.URL.Path[len("/view/"):] 42 | p, err := loadPage(title) 43 | if err != nil { 44 | http.Redirect(w, r, "/edit/"+title, http.StatusFound) 45 | return 46 | } 47 | renderTemplate(w, "view", p) 48 | } 49 | 50 | func editHandler(w http.ResponseWriter, r *http.Request) { 51 | title := r.URL.Path[len("/edit/"):] 52 | p, err := loadPage(title) 53 | if err != nil { 54 | p = &Page{Title: title} 55 | } 56 | renderTemplate(w, "edit", p) 57 | } 58 | 59 | func saveHandler(w http.ResponseWriter, r *http.Request) { 60 | title := r.URL.Path[len("/save/"):] 61 | body := r.FormValue("body") 62 | p := &Page{Title: title, Body: []byte(body)} 63 | err := p.save() 64 | if err != nil { 65 | http.Error(w, err.Error(), http.StatusInternalServerError) 66 | return 67 | } 68 | http.Redirect(w, r, "/view/"+title, http.StatusFound) 69 | } 70 | 71 | func main() { 72 | http.HandleFunc("/view/", viewHandler) 73 | http.HandleFunc("/edit/", editHandler) 74 | http.HandleFunc("/save/", saveHandler) 75 | log.Fatal(http.ListenAndServe(":8080", nil)) 76 | } 77 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/part3.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | import ( 10 | "html/template" 11 | "io/ioutil" 12 | "log" 13 | "net/http" 14 | ) 15 | 16 | type Page struct { 17 | Title string 18 | Body []byte 19 | } 20 | 21 | func (p *Page) save() error { 22 | filename := p.Title + ".txt" 23 | return ioutil.WriteFile(filename, p.Body, 0600) 24 | } 25 | 26 | func loadPage(title string) (*Page, error) { 27 | filename := title + ".txt" 28 | body, err := ioutil.ReadFile(filename) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &Page{Title: title, Body: body}, nil 33 | } 34 | 35 | func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { 36 | t, _ := template.ParseFiles(tmpl + ".html") 37 | t.Execute(w, p) 38 | } 39 | 40 | func viewHandler(w http.ResponseWriter, r *http.Request) { 41 | title := r.URL.Path[len("/view/"):] 42 | p, _ := loadPage(title) 43 | renderTemplate(w, "view", p) 44 | } 45 | 46 | func editHandler(w http.ResponseWriter, r *http.Request) { 47 | title := r.URL.Path[len("/edit/"):] 48 | p, err := loadPage(title) 49 | if err != nil { 50 | p = &Page{Title: title} 51 | } 52 | renderTemplate(w, "edit", p) 53 | } 54 | 55 | func main() { 56 | http.HandleFunc("/view/", viewHandler) 57 | http.HandleFunc("/edit/", editHandler) 58 | //http.HandleFunc("/save/", saveHandler) 59 | log.Fatal(http.ListenAndServe(":8080", nil)) 60 | } 61 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/test_Test.txt.good: -------------------------------------------------------------------------------- 1 | some content -------------------------------------------------------------------------------- /_content/doc/articles/wiki/test_edit.good: -------------------------------------------------------------------------------- 1 |

Editing Test

2 | 3 |
4 |
5 |
6 |
7 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/test_view.good: -------------------------------------------------------------------------------- 1 |

Test

2 | 3 |

[edit]

4 | 5 |
some content
6 | -------------------------------------------------------------------------------- /_content/doc/articles/wiki/view.html: -------------------------------------------------------------------------------- 1 |

{{.Title}}

2 | 3 |

[edit]

4 | 5 |
{{printf "%s" .Body}}
6 | -------------------------------------------------------------------------------- /_content/doc/cmd.html: -------------------------------------------------------------------------------- 1 | 4 | 5 |

6 | There is a suite of programs to build and process Go source code. 7 | Instead of being run directly, programs in the suite are usually invoked 8 | by the go program. 9 |

10 | 11 |

12 | The most common way to run these programs is as a subcommand of the go program, 13 | for instance as go fmt. Run like this, the command operates on 14 | complete packages of Go source code, with the go program invoking the 15 | underlying binary with arguments appropriate to package-level processing. 16 |

17 | 18 |

19 | The programs can also be run as stand-alone binaries, with unmodified arguments, 20 | using the go tool subcommand, such as go tool cgo. 21 | For most commands this is mainly useful for debugging. 22 | Some of the commands, such as pprof, are accessible only through 23 | the go tool subcommand. 24 |

25 | 26 |

27 | Finally the fmt and godoc commands are installed 28 | as regular binaries called gofmt and godoc because 29 | they are so often referenced. 30 |

31 | 32 |

33 | Click on the links for more documentation, invocation methods, and usage details. 34 |

35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 65 | 66 | 67 | 68 | 69 | 70 | 72 | 73 | 74 | 75 | 76 | 77 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 92 | 93 | 94 |
Name    Synopsis
go     47 | The go program manages Go source code and runs the other 48 | commands listed here. 49 | See the command docs for usage 50 | details. 51 |
cgo    Cgo enables the creation of Go packages that call C code.
cover    Cover is a program for creating and analyzing the coverage profiles 64 | generated by "go test -coverprofile".
fix    Fix finds Go programs that use old features of the language and libraries 71 | and rewrites them to use newer ones.
fmt    Fmt formats Go packages, it is also available as an independent 78 | gofmt command with more general options.
godoc    Godoc extracts and generates documentation for Go packages.
vet    Vet examines Go source code and reports suspicious constructs, such as Printf 91 | calls whose arguments do not align with the format string.
95 | 96 |

97 | This is an abridged list. See the full command reference 98 | for documentation of the compilers and more. 99 |

100 | -------------------------------------------------------------------------------- /_content/doc/codewalk/codewalk_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package main_test 6 | 7 | import ( 8 | "bytes" 9 | "os" 10 | "os/exec" 11 | "strings" 12 | "testing" 13 | ) 14 | 15 | // TestMarkov tests the code dependency of markov.xml. 16 | func TestMarkov(t *testing.T) { 17 | cmd := exec.Command("go", "run", "markov.go") 18 | cmd.Stdin = strings.NewReader("foo") 19 | cmd.Stderr = bytes.NewBuffer(nil) 20 | out, err := cmd.Output() 21 | if err != nil { 22 | t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr) 23 | } 24 | 25 | if !bytes.Equal(out, []byte("foo\n")) { 26 | t.Fatalf(`%s with input "foo" did not output "foo":\n%s`, strings.Join(cmd.Args, " "), out) 27 | } 28 | } 29 | 30 | // TestPig tests the code dependency of functions.xml. 31 | func TestPig(t *testing.T) { 32 | cmd := exec.Command("go", "run", "pig.go") 33 | cmd.Stderr = bytes.NewBuffer(nil) 34 | out, err := cmd.Output() 35 | if err != nil { 36 | t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr) 37 | } 38 | 39 | const want = "Wins, losses staying at k = 100: 210/990 (21.2%), 780/990 (78.8%)\n" 40 | if !bytes.Contains(out, []byte(want)) { 41 | t.Fatalf(`%s: unexpected output\ngot:\n%s\nwant output containing:\n%s`, strings.Join(cmd.Args, " "), out, want) 42 | } 43 | } 44 | 45 | // TestURLPoll tests the code dependency of sharemem.xml. 46 | func TestURLPoll(t *testing.T) { 47 | cmd := exec.Command("go", "build", "-o", os.DevNull, "urlpoll.go") 48 | out, err := cmd.CombinedOutput() 49 | if err != nil { 50 | t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, out) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /_content/doc/codewalk/pig.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package main 6 | 7 | import ( 8 | "fmt" 9 | "math/rand" 10 | ) 11 | 12 | const ( 13 | win = 100 // The winning score in a game of Pig 14 | gamesPerSeries = 10 // The number of games per series to simulate 15 | ) 16 | 17 | // A score includes scores accumulated in previous turns for each player, 18 | // as well as the points scored by the current player in this turn. 19 | type score struct { 20 | player, opponent, thisTurn int 21 | } 22 | 23 | // An action transitions stochastically to a resulting score. 24 | type action func(current score) (result score, turnIsOver bool) 25 | 26 | // roll returns the (result, turnIsOver) outcome of simulating a die roll. 27 | // If the roll value is 1, then thisTurn score is abandoned, and the players' 28 | // roles swap. Otherwise, the roll value is added to thisTurn. 29 | func roll(s score) (score, bool) { 30 | outcome := rand.Intn(6) + 1 // A random int in [1, 6] 31 | if outcome == 1 { 32 | return score{s.opponent, s.player, 0}, true 33 | } 34 | return score{s.player, s.opponent, outcome + s.thisTurn}, false 35 | } 36 | 37 | // stay returns the (result, turnIsOver) outcome of staying. 38 | // thisTurn score is added to the player's score, and the players' roles swap. 39 | func stay(s score) (score, bool) { 40 | return score{s.opponent, s.player + s.thisTurn, 0}, true 41 | } 42 | 43 | // A strategy chooses an action for any given score. 44 | type strategy func(score) action 45 | 46 | // stayAtK returns a strategy that rolls until thisTurn is at least k, then stays. 47 | func stayAtK(k int) strategy { 48 | return func(s score) action { 49 | if s.thisTurn >= k { 50 | return stay 51 | } 52 | return roll 53 | } 54 | } 55 | 56 | // play simulates a Pig game and returns the winner (0 or 1). 57 | func play(strategy0, strategy1 strategy) int { 58 | strategies := []strategy{strategy0, strategy1} 59 | var s score 60 | var turnIsOver bool 61 | currentPlayer := rand.Intn(2) // Randomly decide who plays first 62 | for s.player+s.thisTurn < win { 63 | action := strategies[currentPlayer](s) 64 | s, turnIsOver = action(s) 65 | if turnIsOver { 66 | currentPlayer = (currentPlayer + 1) % 2 67 | } 68 | } 69 | return currentPlayer 70 | } 71 | 72 | // roundRobin simulates a series of games between every pair of strategies. 73 | func roundRobin(strategies []strategy) ([]int, int) { 74 | wins := make([]int, len(strategies)) 75 | for i := 0; i < len(strategies); i++ { 76 | for j := i + 1; j < len(strategies); j++ { 77 | for k := 0; k < gamesPerSeries; k++ { 78 | winner := play(strategies[i], strategies[j]) 79 | if winner == 0 { 80 | wins[i]++ 81 | } else { 82 | wins[j]++ 83 | } 84 | } 85 | } 86 | } 87 | gamesPerStrategy := gamesPerSeries * (len(strategies) - 1) // no self play 88 | return wins, gamesPerStrategy 89 | } 90 | 91 | // ratioString takes a list of integer values and returns a string that lists 92 | // each value and its percentage of the sum of all values. 93 | // e.g., ratios(1, 2, 3) = "1/6 (16.7%), 2/6 (33.3%), 3/6 (50.0%)" 94 | func ratioString(vals ...int) string { 95 | total := 0 96 | for _, val := range vals { 97 | total += val 98 | } 99 | s := "" 100 | for _, val := range vals { 101 | if s != "" { 102 | s += ", " 103 | } 104 | pct := 100 * float64(val) / float64(total) 105 | s += fmt.Sprintf("%d/%d (%0.1f%%)", val, total, pct) 106 | } 107 | return s 108 | } 109 | 110 | func main() { 111 | strategies := make([]strategy, win) 112 | for k := range strategies { 113 | strategies[k] = stayAtK(k + 1) 114 | } 115 | wins, games := roundRobin(strategies) 116 | 117 | for k := range strategies { 118 | fmt.Printf("Wins, losses staying at k =% 4d: %s\n", 119 | k+1, ratioString(wins[k], games-wins[k])) 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /_content/doc/codewalk/popout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/website/2438e6b39da44a6c41afca996c9e56695448bada/_content/doc/codewalk/popout.png -------------------------------------------------------------------------------- /_content/doc/codewalk/urlpoll.go: -------------------------------------------------------------------------------- 1 | // Copyright 2010 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package main 6 | 7 | import ( 8 | "log" 9 | "net/http" 10 | "time" 11 | ) 12 | 13 | const ( 14 | numPollers = 2 // number of Poller goroutines to launch 15 | pollInterval = 60 * time.Second // how often to poll each URL 16 | statusInterval = 10 * time.Second // how often to log status to stdout 17 | errTimeout = 10 * time.Second // back-off timeout on error 18 | ) 19 | 20 | var urls = []string{ 21 | "http://www.google.com/", 22 | "http://golang.org/", 23 | "http://blog.golang.org/", 24 | } 25 | 26 | // State represents the last-known state of a URL. 27 | type State struct { 28 | url string 29 | status string 30 | } 31 | 32 | // StateMonitor maintains a map that stores the state of the URLs being 33 | // polled, and prints the current state every updateInterval nanoseconds. 34 | // It returns a chan State to which resource state should be sent. 35 | func StateMonitor(updateInterval time.Duration) chan<- State { 36 | updates := make(chan State) 37 | urlStatus := make(map[string]string) 38 | ticker := time.NewTicker(updateInterval) 39 | go func() { 40 | for { 41 | select { 42 | case <-ticker.C: 43 | logState(urlStatus) 44 | case s := <-updates: 45 | urlStatus[s.url] = s.status 46 | } 47 | } 48 | }() 49 | return updates 50 | } 51 | 52 | // logState prints a state map. 53 | func logState(s map[string]string) { 54 | log.Println("Current state:") 55 | for k, v := range s { 56 | log.Printf(" %s %s", k, v) 57 | } 58 | } 59 | 60 | // Resource represents an HTTP URL to be polled by this program. 61 | type Resource struct { 62 | url string 63 | errCount int 64 | } 65 | 66 | // Poll executes an HTTP HEAD request for url 67 | // and returns the HTTP status string or an error string. 68 | func (r *Resource) Poll() string { 69 | resp, err := http.Head(r.url) 70 | if err != nil { 71 | log.Println("Error", r.url, err) 72 | r.errCount++ 73 | return err.Error() 74 | } 75 | r.errCount = 0 76 | return resp.Status 77 | } 78 | 79 | // Sleep sleeps for an appropriate interval (dependent on error state) 80 | // before sending the Resource to done. 81 | func (r *Resource) Sleep(done chan<- *Resource) { 82 | time.Sleep(pollInterval + errTimeout*time.Duration(r.errCount)) 83 | done <- r 84 | } 85 | 86 | func Poller(in <-chan *Resource, out chan<- *Resource, status chan<- State) { 87 | for r := range in { 88 | s := r.Poll() 89 | status <- State{r.url, s} 90 | out <- r 91 | } 92 | } 93 | 94 | func main() { 95 | // Create our input and output channels. 96 | pending, complete := make(chan *Resource), make(chan *Resource) 97 | 98 | // Launch the StateMonitor. 99 | status := StateMonitor(statusInterval) 100 | 101 | // Launch some Poller goroutines. 102 | for i := 0; i < numPollers; i++ { 103 | go Poller(pending, complete, status) 104 | } 105 | 106 | // Send some Resources to the pending queue. 107 | go func() { 108 | for _, url := range urls { 109 | pending <- &Resource{url: url} 110 | } 111 | }() 112 | 113 | for r := range complete { 114 | go r.Sleep(pending) 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /_content/doc/copyright.html: -------------------------------------------------------------------------------- 1 | 4 | 5 |

6 | Except as 7 | noted, the contents of this 8 | site are licensed under the 9 | Creative Commons Attribution 3.0 License, 10 | and code is licensed under a BSD license. 11 |

12 | -------------------------------------------------------------------------------- /_content/doc/editors.html: -------------------------------------------------------------------------------- 1 | 5 | 6 |

Introduction

7 | 8 |

9 | This document lists commonly used editor plugins and IDEs from the Go ecosystem 10 | that make Go development more productive and seamless. 11 | A comprehensive list of editor support and IDEs for Go development is available at 12 | the wiki. 13 |

14 | 15 |

Options

16 |

17 | The Go ecosystem provides a variety of editor plugins and IDEs to enhance your day-to-day 18 | editing, navigation, testing, and debugging experience. 19 |

20 | 21 |