recover example

This commit is contained in:
kilaka 2021-02-15 10:28:20 +00:00
parent f72f11e641
commit 481091c61f
79 changed files with 427 additions and 76 deletions

View File

@ -41,6 +41,7 @@ Sorting
Sorting by Functions
Panic
Defer
Recover
Collection Functions
String Functions
String Formatting

View File

@ -0,0 +1,49 @@
// A `recover` means recovering from a `panic`, either from a "business" or "built-in" panic.
// We want to recover if we want to handle a panic, stopping it from propagating upwards.
package main
import (
"fmt"
)
func main() {
recoverFromBuiltInPanic(10)
fmt.Println()
recoverFromCustomPanic(-1)
}
func recoverFromBuiltInPanic(i int) {
// defer is defined.
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered. Error:\n", r)
}
}()
var a [5]int
fmt.Printf("Getting index %d"+
" of array of len %d...\n", i, len(a))
fmt.Printf("Item in index %d: %d", i, a[i])
}
func recoverFromCustomPanic(i int) {
// defer is defined.
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered. Error:\n", r)
}
}()
fmt.Printf("About to process i=%d\n", i)
if i < 0 {
panic(fmt.Errorf("Accepting only"+
" non-negative numbers but received %d", i))
}
fmt.Printf("Doing something with %d\n", i)
}

View File

@ -0,0 +1,2 @@
a098d6e6717560d0b67ae579b78e00a74cf8781a
uVo4c0IE97q

View File

@ -0,0 +1,13 @@
# Running this program will exit correctly,
# even though panic was invoked in two methods.
# The recover is responsible for recovering from panics.
$ go run recover.go
Getting index 10 of array of len 5...
Recovered. Error:
runtime error: index out of range [10] with length 5
About to process i=-1
Recovered. Error:
Accepting only non-negative numbers but received -1
# Note that, in Go it is idiomatic
# to use error-indicating return values wherever possible.

2
public/arrays generated
View File

@ -206,7 +206,7 @@ typical Go. We&rsquo;ll look at slices next.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var a [5]int\u000A fmt.Println(\"emp:\", a)\u000A');codeLines.push(' a[4] \x3D 100\u000A fmt.Println(\"set:\", a)\u000A fmt.Println(\"get:\", a[4])\u000A');codeLines.push(' fmt.Println(\"len:\", len(a))\u000A');codeLines.push(' b :\x3D [5]int{1, 2, 3, 4, 5}\u000A fmt.Println(\"dcl:\", b)\u000A');codeLines.push(' var twoD [2][3]int\u000A for i :\x3D 0; i \x3C 2; i++ {\u000A for j :\x3D 0; j \x3C 3; j++ {\u000A twoD[i][j] \x3D i + j\u000A }\u000A }\u000A fmt.Println(\"2d: \", twoD)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var a [5]int\u000A fmt.Println(\"emp:\", a)\u000A');codeLines.push(' a[4] \u003D 100\u000A fmt.Println(\"set:\", a)\u000A fmt.Println(\"get:\", a[4])\u000A');codeLines.push(' fmt.Println(\"len:\", len(a))\u000A');codeLines.push(' b :\u003D [5]int{1, 2, 3, 4, 5}\u000A fmt.Println(\"dcl:\", b)\u000A');codeLines.push(' var twoD [2][3]int\u000A for i :\u003D 0; i \u003C 2; i++ {\u000A for j :\u003D 0; j \u003C 3; j++ {\u000A twoD[i][j] \u003D i + j\u000A }\u000A }\u000A fmt.Println(\"2d: \", twoD)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -236,7 +236,7 @@ state.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sync\"\u000A \"sync/atomic\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var ops uint64\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i :\x3D 0; i \x3C 50; i++ {\u000A wg.Add(1)\u000A');codeLines.push(' go func() {\u000A for c :\x3D 0; c \x3C 1000; c++ {\u000A');codeLines.push(' atomic.AddUint64(\x26ops, 1)\u000A }\u000A wg.Done()\u000A }()\u000A }\u000A');codeLines.push(' wg.Wait()\u000A');codeLines.push(' fmt.Println(\"ops:\", ops)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sync\"\u000A \"sync/atomic\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var ops uint64\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i :\u003D 0; i \u003C 50; i++ {\u000A wg.Add(1)\u000A');codeLines.push(' go func() {\u000A for c :\u003D 0; c \u003C 1000; c++ {\u000A');codeLines.push(' atomic.AddUint64(\u0026ops, 1)\u000A }\u000A wg.Done()\u000A }()\u000A }\u000A');codeLines.push(' wg.Wait()\u000A');codeLines.push(' fmt.Println(\"ops:\", ops)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -191,7 +191,7 @@ but they both decode to the original string as desired.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A b64 \"encoding/base64\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' data :\x3D \"abc123!?$*\x26()\'-\x3D@~\"\u000A');codeLines.push(' sEnc :\x3D b64.StdEncoding.EncodeToString([]byte(data))\u000A fmt.Println(sEnc)\u000A');codeLines.push(' sDec, _ :\x3D b64.StdEncoding.DecodeString(sEnc)\u000A fmt.Println(string(sDec))\u000A fmt.Println()\u000A');codeLines.push(' uEnc :\x3D b64.URLEncoding.EncodeToString([]byte(data))\u000A fmt.Println(uEnc)\u000A uDec, _ :\x3D b64.URLEncoding.DecodeString(uEnc)\u000A fmt.Println(string(uDec))\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A b64 \"encoding/base64\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' data :\u003D \"abc123!?$*\u0026()\'-\u003D@~\"\u000A');codeLines.push(' sEnc :\u003D b64.StdEncoding.EncodeToString([]byte(data))\u000A fmt.Println(sEnc)\u000A');codeLines.push(' sDec, _ :\u003D b64.StdEncoding.DecodeString(sEnc)\u000A fmt.Println(string(sDec))\u000A fmt.Println()\u000A');codeLines.push(' uEnc :\u003D b64.URLEncoding.EncodeToString([]byte(data))\u000A fmt.Println(uEnc)\u000A uDec, _ :\u003D b64.URLEncoding.DecodeString(uEnc)\u000A fmt.Println(string(uDec))\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -153,7 +153,7 @@ concurrent receive.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' messages :\x3D make(chan string, 2)\u000A');codeLines.push(' messages \x3C- \"buffered\"\u000A messages \x3C- \"channel\"\u000A');codeLines.push(' fmt.Println(\x3C-messages)\u000A fmt.Println(\x3C-messages)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' messages :\u003D make(chan string, 2)\u000A');codeLines.push(' messages \u003C- \"buffered\"\u000A messages \u003C- \"channel\"\u000A');codeLines.push(' fmt.Println(\u003C-messages)\u000A fmt.Println(\u003C-messages)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -145,7 +145,7 @@ receive on this channel.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func ping(pings chan\x3C- string, msg string) {\u000A pings \x3C- msg\u000A}\u000A');codeLines.push('func pong(pings \x3C-chan string, pongs chan\x3C- string) {\u000A msg :\x3D \x3C-pings\u000A pongs \x3C- msg\u000A}\u000A');codeLines.push('func main() {\u000A pings :\x3D make(chan string, 1)\u000A pongs :\x3D make(chan string, 1)\u000A ping(pings, \"passed message\")\u000A pong(pings, pongs)\u000A fmt.Println(\x3C-pongs)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func ping(pings chan\u003C- string, msg string) {\u000A pings \u003C- msg\u000A}\u000A');codeLines.push('func pong(pings \u003C-chan string, pongs chan\u003C- string) {\u000A msg :\u003D \u003C-pings\u000A pongs \u003C- msg\u000A}\u000A');codeLines.push('func main() {\u000A pings :\u003D make(chan string, 1)\u000A pongs :\u003D make(chan string, 1)\u000A ping(pings, \"passed message\")\u000A pong(pings, pongs)\u000A fmt.Println(\u003C-pongs)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -184,7 +184,7 @@ started.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func worker(done chan bool) {\u000A fmt.Print(\"working...\")\u000A time.Sleep(time.Second)\u000A fmt.Println(\"done\")\u000A');codeLines.push(' done \x3C- true\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' done :\x3D make(chan bool, 1)\u000A go worker(done)\u000A');codeLines.push(' \x3C-done\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func worker(done chan bool) {\u000A fmt.Print(\"working...\")\u000A time.Sleep(time.Second)\u000A fmt.Println(\"done\")\u000A');codeLines.push(' done \u003C- true\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' done :\u003D make(chan bool, 1)\u000A go worker(done)\u000A');codeLines.push(' \u003C-done\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/channels generated
View File

@ -168,7 +168,7 @@ message without having to use any other synchronization.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' messages :\x3D make(chan string)\u000A');codeLines.push(' go func() { messages \x3C- \"ping\" }()\u000A');codeLines.push(' msg :\x3D \x3C-messages\u000A fmt.Println(msg)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' messages :\u003D make(chan string)\u000A');codeLines.push(' go func() { messages \u003C- \"ping\" }()\u000A');codeLines.push(' msg :\u003D \u003C-messages\u000A fmt.Println(msg)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -194,7 +194,7 @@ example: <code>range</code> over channels.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A jobs :\x3D make(chan int, 5)\u000A done :\x3D make(chan bool)\u000A');codeLines.push(' go func() {\u000A for {\u000A j, more :\x3D \x3C-jobs\u000A if more {\u000A fmt.Println(\"received job\", j)\u000A } else {\u000A fmt.Println(\"received all jobs\")\u000A done \x3C- true\u000A return\u000A }\u000A }\u000A }()\u000A');codeLines.push(' for j :\x3D 1; j \x3C\x3D 3; j++ {\u000A jobs \x3C- j\u000A fmt.Println(\"sent job\", j)\u000A }\u000A close(jobs)\u000A fmt.Println(\"sent all jobs\")\u000A');codeLines.push(' \x3C-done\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A jobs :\u003D make(chan int, 5)\u000A done :\u003D make(chan bool)\u000A');codeLines.push(' go func() {\u000A for {\u000A j, more :\u003D \u003C-jobs\u000A if more {\u000A fmt.Println(\"received job\", j)\u000A } else {\u000A fmt.Println(\"received all jobs\")\u000A done \u003C- true\u000A return\u000A }\u000A }\u000A }()\u000A');codeLines.push(' for j :\u003D 1; j \u003C\u003D 3; j++ {\u000A jobs \u003C- j\u000A fmt.Println(\"sent job\", j)\u000A }\u000A close(jobs)\u000A fmt.Println(\"sent all jobs\")\u000A');codeLines.push(' \u003C-done\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/closures generated
View File

@ -190,7 +190,7 @@ recursion.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func intSeq() func() int {\u000A i :\x3D 0\u000A return func() int {\u000A i++\u000A return i\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' nextInt :\x3D intSeq()\u000A');codeLines.push(' fmt.Println(nextInt())\u000A fmt.Println(nextInt())\u000A fmt.Println(nextInt())\u000A');codeLines.push(' newInts :\x3D intSeq()\u000A fmt.Println(newInts())\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func intSeq() func() int {\u000A i :\u003D 0\u000A return func() int {\u000A i++\u000A return i\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' nextInt :\u003D intSeq()\u000A');codeLines.push(' fmt.Println(nextInt())\u000A fmt.Println(nextInt())\u000A fmt.Println(nextInt())\u000A');codeLines.push(' newInts :\u003D intSeq()\u000A fmt.Println(newInts())\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -9,7 +9,7 @@
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'defer';
window.location.href = 'recover';
}
@ -371,7 +371,7 @@ type.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func Index(vs []string, t string) int {\u000A for i, v :\x3D range vs {\u000A if v \x3D\x3D t {\u000A return i\u000A }\u000A }\u000A return -1\u000A}\u000A');codeLines.push('func Include(vs []string, t string) bool {\u000A return Index(vs, t) \x3E\x3D 0\u000A}\u000A');codeLines.push('func Any(vs []string, f func(string) bool) bool {\u000A for _, v :\x3D range vs {\u000A if f(v) {\u000A return true\u000A }\u000A }\u000A return false\u000A}\u000A');codeLines.push('func All(vs []string, f func(string) bool) bool {\u000A for _, v :\x3D range vs {\u000A if !f(v) {\u000A return false\u000A }\u000A }\u000A return true\u000A}\u000A');codeLines.push('func Filter(vs []string, f func(string) bool) []string {\u000A vsf :\x3D make([]string, 0)\u000A for _, v :\x3D range vs {\u000A if f(v) {\u000A vsf \x3D append(vsf, v)\u000A }\u000A }\u000A return vsf\u000A}\u000A');codeLines.push('func Map(vs []string, f func(string) string) []string {\u000A vsm :\x3D make([]string, len(vs))\u000A for i, v :\x3D range vs {\u000A vsm[i] \x3D f(v)\u000A }\u000A return vsm\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var strs \x3D []string{\"peach\", \"apple\", \"pear\", \"plum\"}\u000A');codeLines.push(' fmt.Println(Index(strs, \"pear\"))\u000A');codeLines.push(' fmt.Println(Include(strs, \"grape\"))\u000A');codeLines.push(' fmt.Println(Any(strs, func(v string) bool {\u000A return strings.HasPrefix(v, \"p\")\u000A }))\u000A');codeLines.push(' fmt.Println(All(strs, func(v string) bool {\u000A return strings.HasPrefix(v, \"p\")\u000A }))\u000A');codeLines.push(' fmt.Println(Filter(strs, func(v string) bool {\u000A return strings.Contains(v, \"e\")\u000A }))\u000A');codeLines.push(' fmt.Println(Map(strs, strings.ToUpper))\u000A');codeLines.push('}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func Index(vs []string, t string) int {\u000A for i, v :\u003D range vs {\u000A if v \u003D\u003D t {\u000A return i\u000A }\u000A }\u000A return -1\u000A}\u000A');codeLines.push('func Include(vs []string, t string) bool {\u000A return Index(vs, t) \u003E\u003D 0\u000A}\u000A');codeLines.push('func Any(vs []string, f func(string) bool) bool {\u000A for _, v :\u003D range vs {\u000A if f(v) {\u000A return true\u000A }\u000A }\u000A return false\u000A}\u000A');codeLines.push('func All(vs []string, f func(string) bool) bool {\u000A for _, v :\u003D range vs {\u000A if !f(v) {\u000A return false\u000A }\u000A }\u000A return true\u000A}\u000A');codeLines.push('func Filter(vs []string, f func(string) bool) []string {\u000A vsf :\u003D make([]string, 0)\u000A for _, v :\u003D range vs {\u000A if f(v) {\u000A vsf \u003D append(vsf, v)\u000A }\u000A }\u000A return vsf\u000A}\u000A');codeLines.push('func Map(vs []string, f func(string) string) []string {\u000A vsm :\u003D make([]string, len(vs))\u000A for i, v :\u003D range vs {\u000A vsm[i] \u003D f(v)\u000A }\u000A return vsm\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var strs \u003D []string{\"peach\", \"apple\", \"pear\", \"plum\"}\u000A');codeLines.push(' fmt.Println(Index(strs, \"pear\"))\u000A');codeLines.push(' fmt.Println(Include(strs, \"grape\"))\u000A');codeLines.push(' fmt.Println(Any(strs, func(v string) bool {\u000A return strings.HasPrefix(v, \"p\")\u000A }))\u000A');codeLines.push(' fmt.Println(All(strs, func(v string) bool {\u000A return strings.HasPrefix(v, \"p\")\u000A }))\u000A');codeLines.push(' fmt.Println(Filter(strs, func(v string) bool {\u000A return strings.Contains(v, \"e\")\u000A }))\u000A');codeLines.push(' fmt.Println(Map(strs, strings.ToUpper))\u000A');codeLines.push('}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -170,7 +170,7 @@ with flags.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' argsWithProg :\x3D os.Args\u000A argsWithoutProg :\x3D os.Args[1:]\u000A');codeLines.push(' arg :\x3D os.Args[3]\u000A');codeLines.push(' fmt.Println(argsWithProg)\u000A fmt.Println(argsWithoutProg)\u000A fmt.Println(arg)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' argsWithProg :\u003D os.Args\u000A argsWithoutProg :\u003D os.Args[1:]\u000A');codeLines.push(' arg :\u003D os.Args[3]\u000A');codeLines.push(' fmt.Println(argsWithProg)\u000A fmt.Println(argsWithoutProg)\u000A fmt.Println(arg)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -310,7 +310,7 @@ and show the help text again.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"flag\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' wordPtr :\x3D flag.String(\"word\", \"foo\", \"a string\")\u000A');codeLines.push(' numbPtr :\x3D flag.Int(\"numb\", 42, \"an int\")\u000A boolPtr :\x3D flag.Bool(\"fork\", false, \"a bool\")\u000A');codeLines.push(' var svar string\u000A flag.StringVar(\x26svar, \"svar\", \"bar\", \"a string var\")\u000A');codeLines.push(' flag.Parse()\u000A');codeLines.push(' fmt.Println(\"word:\", *wordPtr)\u000A fmt.Println(\"numb:\", *numbPtr)\u000A fmt.Println(\"fork:\", *boolPtr)\u000A fmt.Println(\"svar:\", svar)\u000A fmt.Println(\"tail:\", flag.Args())\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"flag\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' wordPtr :\u003D flag.String(\"word\", \"foo\", \"a string\")\u000A');codeLines.push(' numbPtr :\u003D flag.Int(\"numb\", 42, \"an int\")\u000A boolPtr :\u003D flag.Bool(\"fork\", false, \"a bool\")\u000A');codeLines.push(' var svar string\u000A flag.StringVar(\u0026svar, \"svar\", \"bar\", \"a string var\")\u000A');codeLines.push(' flag.Parse()\u000A');codeLines.push(' fmt.Println(\"word:\", *wordPtr)\u000A fmt.Println(\"numb:\", *numbPtr)\u000A fmt.Println(\"fork:\", *boolPtr)\u000A fmt.Println(\"svar:\", svar)\u000A fmt.Println(\"tail:\", flag.Args())\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -263,7 +263,7 @@ way to parameterize programs.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"flag\"\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fooCmd :\x3D flag.NewFlagSet(\"foo\", flag.ExitOnError)\u000A fooEnable :\x3D fooCmd.Bool(\"enable\", false, \"enable\")\u000A fooName :\x3D fooCmd.String(\"name\", \"\", \"name\")\u000A');codeLines.push(' barCmd :\x3D flag.NewFlagSet(\"bar\", flag.ExitOnError)\u000A barLevel :\x3D barCmd.Int(\"level\", 0, \"level\")\u000A');codeLines.push(' if len(os.Args) \x3C 2 {\u000A fmt.Println(\"expected \'foo\' or \'bar\' subcommands\")\u000A os.Exit(1)\u000A }\u000A');codeLines.push(' switch os.Args[1] {\u000A');codeLines.push(' case \"foo\":\u000A fooCmd.Parse(os.Args[2:])\u000A fmt.Println(\"subcommand \'foo\'\")\u000A fmt.Println(\" enable:\", *fooEnable)\u000A fmt.Println(\" name:\", *fooName)\u000A fmt.Println(\" tail:\", fooCmd.Args())\u000A case \"bar\":\u000A barCmd.Parse(os.Args[2:])\u000A fmt.Println(\"subcommand \'bar\'\")\u000A fmt.Println(\" level:\", *barLevel)\u000A fmt.Println(\" tail:\", barCmd.Args())\u000A default:\u000A fmt.Println(\"expected \'foo\' or \'bar\' subcommands\")\u000A os.Exit(1)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"flag\"\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fooCmd :\u003D flag.NewFlagSet(\"foo\", flag.ExitOnError)\u000A fooEnable :\u003D fooCmd.Bool(\"enable\", false, \"enable\")\u000A fooName :\u003D fooCmd.String(\"name\", \"\", \"name\")\u000A');codeLines.push(' barCmd :\u003D flag.NewFlagSet(\"bar\", flag.ExitOnError)\u000A barLevel :\u003D barCmd.Int(\"level\", 0, \"level\")\u000A');codeLines.push(' if len(os.Args) \u003C 2 {\u000A fmt.Println(\"expected \'foo\' or \'bar\' subcommands\")\u000A os.Exit(1)\u000A }\u000A');codeLines.push(' switch os.Args[1] {\u000A');codeLines.push(' case \"foo\":\u000A fooCmd.Parse(os.Args[2:])\u000A fmt.Println(\"subcommand \'foo\'\")\u000A fmt.Println(\" enable:\", *fooEnable)\u000A fmt.Println(\" name:\", *fooName)\u000A fmt.Println(\" tail:\", fooCmd.Args())\u000A case \"bar\":\u000A barCmd.Parse(os.Args[2:])\u000A fmt.Println(\"subcommand \'bar\'\")\u000A fmt.Println(\" level:\", *barLevel)\u000A fmt.Println(\" tail:\", barCmd.Args())\u000A default:\u000A fmt.Println(\"expected \'foo\' or \'bar\' subcommands\")\u000A os.Exit(1)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/constants generated
View File

@ -183,7 +183,7 @@ assignment or function call. For example, here
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math\"\u000A)\u000A');codeLines.push('const s string \x3D \"constant\"\u000A');codeLines.push('func main() {\u000A fmt.Println(s)\u000A');codeLines.push(' const n \x3D 500000000\u000A');codeLines.push(' const d \x3D 3e20 / n\u000A fmt.Println(d)\u000A');codeLines.push(' fmt.Println(int64(d))\u000A');codeLines.push(' fmt.Println(math.Sin(n))\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math\"\u000A)\u000A');codeLines.push('const s string \u003D \"constant\"\u000A');codeLines.push('func main() {\u000A fmt.Println(s)\u000A');codeLines.push(' const n \u003D 500000000\u000A');codeLines.push(' const d \u003D 3e20 / n\u000A fmt.Println(d)\u000A');codeLines.push(' fmt.Println(int64(d))\u000A');codeLines.push(' fmt.Println(math.Sin(n))\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/context generated
View File

@ -205,7 +205,7 @@ cancellation.</p>
</div>
<script>
var codeLines = [];
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A \"time\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' ctx :\x3D req.Context()\u000A fmt.Println(\"server: hello handler started\")\u000A defer fmt.Println(\"server: hello handler ended\")\u000A');codeLines.push(' select {\u000A case \x3C-time.After(10 * time.Second):\u000A fmt.Fprintf(w, \"hello\\n\")\u000A case \x3C-ctx.Done():\u000A');codeLines.push(' err :\x3D ctx.Err()\u000A fmt.Println(\"server:\", err)\u000A internalError :\x3D http.StatusInternalServerError\u000A http.Error(w, err.Error(), internalError)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A \"time\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' ctx :\u003D req.Context()\u000A fmt.Println(\"server: hello handler started\")\u000A defer fmt.Println(\"server: hello handler ended\")\u000A');codeLines.push(' select {\u000A case \u003C-time.After(10 * time.Second):\u000A fmt.Fprintf(w, \"hello\\n\")\u000A case \u003C-ctx.Done():\u000A');codeLines.push(' err :\u003D ctx.Err()\u000A fmt.Println(\"server:\", err)\u000A internalError :\u003D http.StatusInternalServerError\u000A http.Error(w, err.Error(), internalError)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

6
public/defer generated
View File

@ -14,7 +14,7 @@
if (e.key == "ArrowRight") {
window.location.href = 'collection-functions';
window.location.href = 'recover';
}
}
@ -203,7 +203,7 @@ after being written.</p>
<p class="next">
Next example: <a href="collection-functions">Collection Functions</a>.
Next example: <a href="recover">Recover</a>.
</p>
<p class="footer">
@ -212,7 +212,7 @@ after being written.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f :\x3D createFile(\"/tmp/defer.txt\")\u000A defer closeFile(f)\u000A writeFile(f)\u000A}\u000A');codeLines.push('func createFile(p string) *os.File {\u000A fmt.Println(\"creating\")\u000A f, err :\x3D os.Create(p)\u000A if err !\x3D nil {\u000A panic(err)\u000A }\u000A return f\u000A}\u000A');codeLines.push('func writeFile(f *os.File) {\u000A fmt.Println(\"writing\")\u000A fmt.Fprintln(f, \"data\")\u000A');codeLines.push('}\u000A');codeLines.push('func closeFile(f *os.File) {\u000A fmt.Println(\"closing\")\u000A err :\x3D f.Close()\u000A');codeLines.push(' if err !\x3D nil {\u000A fmt.Fprintf(os.Stderr, \"error: %v\\n\", err)\u000A os.Exit(1)\u000A }\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f :\u003D createFile(\"/tmp/defer.txt\")\u000A defer closeFile(f)\u000A writeFile(f)\u000A}\u000A');codeLines.push('func createFile(p string) *os.File {\u000A fmt.Println(\"creating\")\u000A f, err :\u003D os.Create(p)\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A return f\u000A}\u000A');codeLines.push('func writeFile(f *os.File) {\u000A fmt.Println(\"writing\")\u000A fmt.Fprintln(f, \"data\")\u000A');codeLines.push('}\u000A');codeLines.push('func closeFile(f *os.File) {\u000A fmt.Println(\"closing\")\u000A err :\u003D f.Close()\u000A');codeLines.push(' if err !\u003D nil {\u000A fmt.Fprintf(os.Stderr, \"error: %v\\n\", err)\u000A os.Exit(1)\u000A }\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/directories generated
View File

@ -353,7 +353,7 @@ recursively by <code>filepath.Walk</code>.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"io/ioutil\"\u000A \"os\"\u000A \"path/filepath\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\x3D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' err :\x3D os.Mkdir(\"subdir\", 0755)\u000A check(err)\u000A');codeLines.push(' defer os.RemoveAll(\"subdir\")\u000A');codeLines.push(' createEmptyFile :\x3D func(name string) {\u000A d :\x3D []byte(\"\")\u000A check(ioutil.WriteFile(name, d, 0644))\u000A }\u000A');codeLines.push(' createEmptyFile(\"subdir/file1\")\u000A');codeLines.push(' err \x3D os.MkdirAll(\"subdir/parent/child\", 0755)\u000A check(err)\u000A');codeLines.push(' createEmptyFile(\"subdir/parent/file2\")\u000A createEmptyFile(\"subdir/parent/file3\")\u000A createEmptyFile(\"subdir/parent/child/file4\")\u000A');codeLines.push(' c, err :\x3D ioutil.ReadDir(\"subdir/parent\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Listing subdir/parent\")\u000A for _, entry :\x3D range c {\u000A fmt.Println(\" \", entry.Name(), entry.IsDir())\u000A }\u000A');codeLines.push(' err \x3D os.Chdir(\"subdir/parent/child\")\u000A check(err)\u000A');codeLines.push(' c, err \x3D ioutil.ReadDir(\".\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Listing subdir/parent/child\")\u000A for _, entry :\x3D range c {\u000A fmt.Println(\" \", entry.Name(), entry.IsDir())\u000A }\u000A');codeLines.push(' err \x3D os.Chdir(\"../../..\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Visiting subdir\")\u000A err \x3D filepath.Walk(\"subdir\", visit)\u000A}\u000A');codeLines.push('func visit(p string, info os.FileInfo, err error) error {\u000A if err !\x3D nil {\u000A return err\u000A }\u000A fmt.Println(\" \", p, info.IsDir())\u000A return nil\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"io/ioutil\"\u000A \"os\"\u000A \"path/filepath\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\u003D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' err :\u003D os.Mkdir(\"subdir\", 0755)\u000A check(err)\u000A');codeLines.push(' defer os.RemoveAll(\"subdir\")\u000A');codeLines.push(' createEmptyFile :\u003D func(name string) {\u000A d :\u003D []byte(\"\")\u000A check(ioutil.WriteFile(name, d, 0644))\u000A }\u000A');codeLines.push(' createEmptyFile(\"subdir/file1\")\u000A');codeLines.push(' err \u003D os.MkdirAll(\"subdir/parent/child\", 0755)\u000A check(err)\u000A');codeLines.push(' createEmptyFile(\"subdir/parent/file2\")\u000A createEmptyFile(\"subdir/parent/file3\")\u000A createEmptyFile(\"subdir/parent/child/file4\")\u000A');codeLines.push(' c, err :\u003D ioutil.ReadDir(\"subdir/parent\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Listing subdir/parent\")\u000A for _, entry :\u003D range c {\u000A fmt.Println(\" \", entry.Name(), entry.IsDir())\u000A }\u000A');codeLines.push(' err \u003D os.Chdir(\"subdir/parent/child\")\u000A check(err)\u000A');codeLines.push(' c, err \u003D ioutil.ReadDir(\".\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Listing subdir/parent/child\")\u000A for _, entry :\u003D range c {\u000A fmt.Println(\" \", entry.Name(), entry.IsDir())\u000A }\u000A');codeLines.push(' err \u003D os.Chdir(\"../../..\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Visiting subdir\")\u000A err \u003D filepath.Walk(\"subdir\", visit)\u000A}\u000A');codeLines.push('func visit(p string, info os.FileInfo, err error) error {\u000A if err !\u003D nil {\u000A return err\u000A }\u000A fmt.Println(\" \", p, info.IsDir())\u000A return nil\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -186,7 +186,7 @@ program picks that value up.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' os.Setenv(\"FOO\", \"1\")\u000A fmt.Println(\"FOO:\", os.Getenv(\"FOO\"))\u000A fmt.Println(\"BAR:\", os.Getenv(\"BAR\"))\u000A');codeLines.push(' fmt.Println()\u000A for _, e :\x3D range os.Environ() {\u000A pair :\x3D strings.SplitN(e, \"\x3D\", 2)\u000A fmt.Println(pair[0])\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' os.Setenv(\"FOO\", \"1\")\u000A fmt.Println(\"FOO:\", os.Getenv(\"FOO\"))\u000A fmt.Println(\"BAR:\", os.Getenv(\"BAR\"))\u000A');codeLines.push(' fmt.Println()\u000A for _, e :\u003D range os.Environ() {\u000A pair :\u003D strings.SplitN(e, \"\u003D\", 2)\u000A fmt.Println(pair[0])\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/epoch generated
View File

@ -177,7 +177,7 @@ parsing and formatting.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' now :\x3D time.Now()\u000A secs :\x3D now.Unix()\u000A nanos :\x3D now.UnixNano()\u000A fmt.Println(now)\u000A');codeLines.push(' millis :\x3D nanos / 1000000\u000A fmt.Println(secs)\u000A fmt.Println(millis)\u000A fmt.Println(nanos)\u000A');codeLines.push(' fmt.Println(time.Unix(secs, 0))\u000A fmt.Println(time.Unix(0, nanos))\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' now :\u003D time.Now()\u000A secs :\u003D now.Unix()\u000A nanos :\u003D now.UnixNano()\u000A fmt.Println(now)\u000A');codeLines.push(' millis :\u003D nanos / 1000000\u000A fmt.Println(secs)\u000A fmt.Println(millis)\u000A fmt.Println(nanos)\u000A');codeLines.push(' fmt.Println(time.Unix(secs, 0))\u000A fmt.Println(time.Unix(0, nanos))\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/errors generated
View File

@ -299,7 +299,7 @@ on the Go blog for more on error handling.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"errors\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func f1(arg int) (int, error) {\u000A if arg \x3D\x3D 42 {\u000A');codeLines.push(' return -1, errors.New(\"can\'t work with 42\")\u000A');codeLines.push(' }\u000A');codeLines.push(' return arg + 3, nil\u000A}\u000A');codeLines.push('type argError struct {\u000A arg int\u000A prob string\u000A}\u000A');codeLines.push('func (e *argError) Error() string {\u000A return fmt.Sprintf(\"%d - %s\", e.arg, e.prob)\u000A}\u000A');codeLines.push('func f2(arg int) (int, error) {\u000A if arg \x3D\x3D 42 {\u000A');codeLines.push(' return -1, \x26argError{arg, \"can\'t work with it\"}\u000A }\u000A return arg + 3, nil\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' for _, i :\x3D range []int{7, 42} {\u000A if r, e :\x3D f1(i); e !\x3D nil {\u000A fmt.Println(\"f1 failed:\", e)\u000A } else {\u000A fmt.Println(\"f1 worked:\", r)\u000A }\u000A }\u000A for _, i :\x3D range []int{7, 42} {\u000A if r, e :\x3D f2(i); e !\x3D nil {\u000A fmt.Println(\"f2 failed:\", e)\u000A } else {\u000A fmt.Println(\"f2 worked:\", r)\u000A }\u000A }\u000A');codeLines.push(' _, e :\x3D f2(42)\u000A if ae, ok :\x3D e.(*argError); ok {\u000A fmt.Println(ae.arg)\u000A fmt.Println(ae.prob)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"errors\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func f1(arg int) (int, error) {\u000A if arg \u003D\u003D 42 {\u000A');codeLines.push(' return -1, errors.New(\"can\'t work with 42\")\u000A');codeLines.push(' }\u000A');codeLines.push(' return arg + 3, nil\u000A}\u000A');codeLines.push('type argError struct {\u000A arg int\u000A prob string\u000A}\u000A');codeLines.push('func (e *argError) Error() string {\u000A return fmt.Sprintf(\"%d - %s\", e.arg, e.prob)\u000A}\u000A');codeLines.push('func f2(arg int) (int, error) {\u000A if arg \u003D\u003D 42 {\u000A');codeLines.push(' return -1, \u0026argError{arg, \"can\'t work with it\"}\u000A }\u000A return arg + 3, nil\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' for _, i :\u003D range []int{7, 42} {\u000A if r, e :\u003D f1(i); e !\u003D nil {\u000A fmt.Println(\"f1 failed:\", e)\u000A } else {\u000A fmt.Println(\"f1 worked:\", r)\u000A }\u000A }\u000A for _, i :\u003D range []int{7, 42} {\u000A if r, e :\u003D f2(i); e !\u003D nil {\u000A fmt.Println(\"f2 failed:\", e)\u000A } else {\u000A fmt.Println(\"f2 worked:\", r)\u000A }\u000A }\u000A');codeLines.push(' _, e :\u003D f2(42)\u000A if ae, ok :\u003D e.(*argError); ok {\u000A fmt.Println(ae.arg)\u000A fmt.Println(ae.prob)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -203,7 +203,7 @@ processes covers most use cases for <code>fork</code>.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"os\"\u000A \"os/exec\"\u000A \"syscall\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' binary, lookErr :\x3D exec.LookPath(\"ls\")\u000A if lookErr !\x3D nil {\u000A panic(lookErr)\u000A }\u000A');codeLines.push(' args :\x3D []string{\"ls\", \"-a\", \"-l\", \"-h\"}\u000A');codeLines.push(' env :\x3D os.Environ()\u000A');codeLines.push(' execErr :\x3D syscall.Exec(binary, args, env)\u000A if execErr !\x3D nil {\u000A panic(execErr)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"os\"\u000A \"os/exec\"\u000A \"syscall\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' binary, lookErr :\u003D exec.LookPath(\"ls\")\u000A if lookErr !\u003D nil {\u000A panic(lookErr)\u000A }\u000A');codeLines.push(' args :\u003D []string{\"ls\", \"-a\", \"-l\", \"-h\"}\u000A');codeLines.push(' env :\u003D os.Environ()\u000A');codeLines.push(' execErr :\u003D syscall.Exec(binary, args, env)\u000A if execErr !\u003D nil {\u000A panic(execErr)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/file-paths generated
View File

@ -250,7 +250,7 @@ be made relative to base.</p>
</div>
<script>
var codeLines = [];
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"path/filepath\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p :\x3D filepath.Join(\"dir1\", \"dir2\", \"filename\")\u000A fmt.Println(\"p:\", p)\u000A');codeLines.push(' fmt.Println(filepath.Join(\"dir1//\", \"filename\"))\u000A fmt.Println(filepath.Join(\"dir1/../dir1\", \"filename\"))\u000A');codeLines.push(' fmt.Println(\"Dir(p):\", filepath.Dir(p))\u000A fmt.Println(\"Base(p):\", filepath.Base(p))\u000A');codeLines.push(' fmt.Println(filepath.IsAbs(\"dir/file\"))\u000A fmt.Println(filepath.IsAbs(\"/dir/file\"))\u000A');codeLines.push(' filename :\x3D \"config.json\"\u000A');codeLines.push(' ext :\x3D filepath.Ext(filename)\u000A fmt.Println(ext)\u000A');codeLines.push(' fmt.Println(strings.TrimSuffix(filename, ext))\u000A');codeLines.push(' rel, err :\x3D filepath.Rel(\"a/b\", \"a/b/t/file\")\u000A if err !\x3D nil {\u000A panic(err)\u000A }\u000A fmt.Println(rel)\u000A');codeLines.push(' rel, err \x3D filepath.Rel(\"a/b\", \"a/c/t/file\")\u000A if err !\x3D nil {\u000A panic(err)\u000A }\u000A fmt.Println(rel)\u000A}\u000A');codeLines.push('');
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"path/filepath\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p :\u003D filepath.Join(\"dir1\", \"dir2\", \"filename\")\u000A fmt.Println(\"p:\", p)\u000A');codeLines.push(' fmt.Println(filepath.Join(\"dir1//\", \"filename\"))\u000A fmt.Println(filepath.Join(\"dir1/../dir1\", \"filename\"))\u000A');codeLines.push(' fmt.Println(\"Dir(p):\", filepath.Dir(p))\u000A fmt.Println(\"Base(p):\", filepath.Base(p))\u000A');codeLines.push(' fmt.Println(filepath.IsAbs(\"dir/file\"))\u000A fmt.Println(filepath.IsAbs(\"/dir/file\"))\u000A');codeLines.push(' filename :\u003D \"config.json\"\u000A');codeLines.push(' ext :\u003D filepath.Ext(filename)\u000A fmt.Println(ext)\u000A');codeLines.push(' fmt.Println(strings.TrimSuffix(filename, ext))\u000A');codeLines.push(' rel, err :\u003D filepath.Rel(\"a/b\", \"a/b/t/file\")\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(rel)\u000A');codeLines.push(' rel, err \u003D filepath.Rel(\"a/b\", \"a/c/t/file\")\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(rel)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/for generated
View File

@ -195,7 +195,7 @@ structures.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' i :\x3D 1\u000A for i \x3C\x3D 3 {\u000A fmt.Println(i)\u000A i \x3D i + 1\u000A }\u000A');codeLines.push(' for j :\x3D 7; j \x3C\x3D 9; j++ {\u000A fmt.Println(j)\u000A }\u000A');codeLines.push(' for {\u000A fmt.Println(\"loop\")\u000A break\u000A }\u000A');codeLines.push(' for n :\x3D 0; n \x3C\x3D 5; n++ {\u000A if n%2 \x3D\x3D 0 {\u000A continue\u000A }\u000A fmt.Println(n)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' i :\u003D 1\u000A for i \u003C\u003D 3 {\u000A fmt.Println(i)\u000A i \u003D i + 1\u000A }\u000A');codeLines.push(' for j :\u003D 7; j \u003C\u003D 9; j++ {\u000A fmt.Println(j)\u000A }\u000A');codeLines.push(' for {\u000A fmt.Println(\"loop\")\u000A break\u000A }\u000A');codeLines.push(' for n :\u003D 0; n \u003C\u003D 5; n++ {\u000A if n%2 \u003D\u003D 0 {\u000A continue\u000A }\u000A fmt.Println(n)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/functions generated
View File

@ -193,7 +193,7 @@ multiple return values, which we&rsquo;ll look at next.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func plus(a int, b int) int {\u000A');codeLines.push(' return a + b\u000A}\u000A');codeLines.push('func plusPlus(a, b, c int) int {\u000A return a + b + c\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' res :\x3D plus(1, 2)\u000A fmt.Println(\"1+2 \x3D\", res)\u000A');codeLines.push(' res \x3D plusPlus(1, 2, 3)\u000A fmt.Println(\"1+2+3 \x3D\", res)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func plus(a int, b int) int {\u000A');codeLines.push(' return a + b\u000A}\u000A');codeLines.push('func plusPlus(a, b, c int) int {\u000A return a + b + c\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' res :\u003D plus(1, 2)\u000A fmt.Println(\"1+2 \u003D\", res)\u000A');codeLines.push(' res \u003D plusPlus(1, 2, 3)\u000A fmt.Println(\"1+2+3 \u003D\", res)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/goroutines generated
View File

@ -207,7 +207,7 @@ concurrent Go programs: channels.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func f(from string) {\u000A for i :\x3D 0; i \x3C 3; i++ {\u000A fmt.Println(from, \":\", i)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f(\"direct\")\u000A');codeLines.push(' go f(\"goroutine\")\u000A');codeLines.push(' go func(msg string) {\u000A fmt.Println(msg)\u000A }(\"going\")\u000A');codeLines.push(' time.Sleep(time.Second)\u000A fmt.Println(\"done\")\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func f(from string) {\u000A for i :\u003D 0; i \u003C 3; i++ {\u000A fmt.Println(from, \":\", i)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f(\"direct\")\u000A');codeLines.push(' go f(\"goroutine\")\u000A');codeLines.push(' go func(msg string) {\u000A fmt.Println(msg)\u000A }(\"going\")\u000A');codeLines.push(' time.Sleep(time.Second)\u000A fmt.Println(\"done\")\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/http-clients generated
View File

@ -169,7 +169,7 @@ settings.</p>
</div>
<script>
var codeLines = [];
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"net/http\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' resp, err :\x3D http.Get(\"http://gobyexample.com\")\u000A if err !\x3D nil {\u000A panic(err)\u000A }\u000A defer resp.Body.Close()\u000A');codeLines.push(' fmt.Println(\"Response status:\", resp.Status)\u000A');codeLines.push(' scanner :\x3D bufio.NewScanner(resp.Body)\u000A for i :\x3D 0; scanner.Scan() \x26\x26 i \x3C 5; i++ {\u000A fmt.Println(scanner.Text())\u000A }\u000A');codeLines.push(' if err :\x3D scanner.Err(); err !\x3D nil {\u000A panic(err)\u000A }\u000A}\u000A');codeLines.push('');
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"net/http\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' resp, err :\u003D http.Get(\"http://gobyexample.com\")\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A defer resp.Body.Close()\u000A');codeLines.push(' fmt.Println(\"Response status:\", resp.Status)\u000A');codeLines.push(' scanner :\u003D bufio.NewScanner(resp.Body)\u000A for i :\u003D 0; scanner.Scan() \u0026\u0026 i \u003C 5; i++ {\u000A fmt.Println(scanner.Text())\u000A }\u000A');codeLines.push(' if err :\u003D scanner.Err(); err !\u003D nil {\u000A panic(err)\u000A }\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/http-servers generated
View File

@ -210,7 +210,7 @@ router we&rsquo;ve just set up.</p>
</div>
<script>
var codeLines = [];
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' fmt.Fprintf(w, \"hello\\n\")\u000A}\u000A');codeLines.push('func headers(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' for name, headers :\x3D range req.Header {\u000A for _, h :\x3D range headers {\u000A fmt.Fprintf(w, \"%v: %v\\n\", name, h)\u000A }\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.HandleFunc(\"/headers\", headers)\u000A');codeLines.push(' http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' fmt.Fprintf(w, \"hello\\n\")\u000A}\u000A');codeLines.push('func headers(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' for name, headers :\u003D range req.Header {\u000A for _, h :\u003D range headers {\u000A fmt.Fprintf(w, \"%v: %v\\n\", name, h)\u000A }\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.HandleFunc(\"/headers\", headers)\u000A');codeLines.push(' http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/if-else generated
View File

@ -184,7 +184,7 @@ for basic conditions.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' if 7%2 \x3D\x3D 0 {\u000A fmt.Println(\"7 is even\")\u000A } else {\u000A fmt.Println(\"7 is odd\")\u000A }\u000A');codeLines.push(' if 8%4 \x3D\x3D 0 {\u000A fmt.Println(\"8 is divisible by 4\")\u000A }\u000A');codeLines.push(' if num :\x3D 9; num \x3C 0 {\u000A fmt.Println(num, \"is negative\")\u000A } else if num \x3C 10 {\u000A fmt.Println(num, \"has 1 digit\")\u000A } else {\u000A fmt.Println(num, \"has multiple digits\")\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' if 7%2 \u003D\u003D 0 {\u000A fmt.Println(\"7 is even\")\u000A } else {\u000A fmt.Println(\"7 is odd\")\u000A }\u000A');codeLines.push(' if 8%4 \u003D\u003D 0 {\u000A fmt.Println(\"8 is divisible by 4\")\u000A }\u000A');codeLines.push(' if num :\u003D 9; num \u003C 0 {\u000A fmt.Println(num, \"is negative\")\u000A } else if num \u003C 10 {\u000A fmt.Println(num, \"has 1 digit\")\u000A } else {\u000A fmt.Println(num, \"has multiple digits\")\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/index.html generated
View File

@ -109,6 +109,8 @@
<li><a href="defer">Defer</a></li>
<li><a href="recover">Recover</a></li>
<li><a href="collection-functions">Collection Functions</a></li>
<li><a href="string-functions">String Functions</a></li>

2
public/interfaces generated
View File

@ -236,7 +236,7 @@ these structs as arguments to <code>measure</code>.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math\"\u000A)\u000A');codeLines.push('type geometry interface {\u000A area() float64\u000A perim() float64\u000A}\u000A');codeLines.push('type rect struct {\u000A width, height float64\u000A}\u000Atype circle struct {\u000A radius float64\u000A}\u000A');codeLines.push('func (r rect) area() float64 {\u000A return r.width * r.height\u000A}\u000Afunc (r rect) perim() float64 {\u000A return 2*r.width + 2*r.height\u000A}\u000A');codeLines.push('func (c circle) area() float64 {\u000A return math.Pi * c.radius * c.radius\u000A}\u000Afunc (c circle) perim() float64 {\u000A return 2 * math.Pi * c.radius\u000A}\u000A');codeLines.push('func measure(g geometry) {\u000A fmt.Println(g)\u000A fmt.Println(g.area())\u000A fmt.Println(g.perim())\u000A}\u000A');codeLines.push('func main() {\u000A r :\x3D rect{width: 3, height: 4}\u000A c :\x3D circle{radius: 5}\u000A');codeLines.push(' measure(r)\u000A measure(c)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math\"\u000A)\u000A');codeLines.push('type geometry interface {\u000A area() float64\u000A perim() float64\u000A}\u000A');codeLines.push('type rect struct {\u000A width, height float64\u000A}\u000Atype circle struct {\u000A radius float64\u000A}\u000A');codeLines.push('func (r rect) area() float64 {\u000A return r.width * r.height\u000A}\u000Afunc (r rect) perim() float64 {\u000A return 2*r.width + 2*r.height\u000A}\u000A');codeLines.push('func (c circle) area() float64 {\u000A return math.Pi * c.radius * c.radius\u000A}\u000Afunc (c circle) perim() float64 {\u000A return 2 * math.Pi * c.radius\u000A}\u000A');codeLines.push('func measure(g geometry) {\u000A fmt.Println(g)\u000A fmt.Println(g.area())\u000A fmt.Println(g.perim())\u000A}\u000A');codeLines.push('func main() {\u000A r :\u003D rect{width: 3, height: 4}\u000A c :\u003D circle{radius: 5}\u000A');codeLines.push(' measure(r)\u000A measure(c)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/json generated
View File

@ -416,7 +416,7 @@ for more.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"encoding/json\"\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('type response1 struct {\u000A Page int\u000A Fruits []string\u000A}\u000A');codeLines.push('type response2 struct {\u000A Page int `json:\"page\"`\u000A Fruits []string `json:\"fruits\"`\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' bolB, _ :\x3D json.Marshal(true)\u000A fmt.Println(string(bolB))\u000A');codeLines.push(' intB, _ :\x3D json.Marshal(1)\u000A fmt.Println(string(intB))\u000A');codeLines.push(' fltB, _ :\x3D json.Marshal(2.34)\u000A fmt.Println(string(fltB))\u000A');codeLines.push(' strB, _ :\x3D json.Marshal(\"gopher\")\u000A fmt.Println(string(strB))\u000A');codeLines.push(' slcD :\x3D []string{\"apple\", \"peach\", \"pear\"}\u000A slcB, _ :\x3D json.Marshal(slcD)\u000A fmt.Println(string(slcB))\u000A');codeLines.push(' mapD :\x3D map[string]int{\"apple\": 5, \"lettuce\": 7}\u000A mapB, _ :\x3D json.Marshal(mapD)\u000A fmt.Println(string(mapB))\u000A');codeLines.push(' res1D :\x3D \x26response1{\u000A Page: 1,\u000A Fruits: []string{\"apple\", \"peach\", \"pear\"}}\u000A res1B, _ :\x3D json.Marshal(res1D)\u000A fmt.Println(string(res1B))\u000A');codeLines.push(' res2D :\x3D \x26response2{\u000A Page: 1,\u000A Fruits: []string{\"apple\", \"peach\", \"pear\"}}\u000A res2B, _ :\x3D json.Marshal(res2D)\u000A fmt.Println(string(res2B))\u000A');codeLines.push(' byt :\x3D []byte(`{\"num\":6.13,\"strs\":[\"a\",\"b\"]}`)\u000A');codeLines.push(' var dat map[string]interface{}\u000A');codeLines.push(' if err :\x3D json.Unmarshal(byt, \x26dat); err !\x3D nil {\u000A panic(err)\u000A }\u000A fmt.Println(dat)\u000A');codeLines.push(' num :\x3D dat[\"num\"].(float64)\u000A fmt.Println(num)\u000A');codeLines.push(' strs :\x3D dat[\"strs\"].([]interface{})\u000A str1 :\x3D strs[0].(string)\u000A fmt.Println(str1)\u000A');codeLines.push(' str :\x3D `{\"page\": 1, \"fruits\": [\"apple\", \"peach\"]}`\u000A res :\x3D response2{}\u000A json.Unmarshal([]byte(str), \x26res)\u000A fmt.Println(res)\u000A fmt.Println(res.Fruits[0])\u000A');codeLines.push(' enc :\x3D json.NewEncoder(os.Stdout)\u000A d :\x3D map[string]int{\"apple\": 5, \"lettuce\": 7}\u000A enc.Encode(d)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"encoding/json\"\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('type response1 struct {\u000A Page int\u000A Fruits []string\u000A}\u000A');codeLines.push('type response2 struct {\u000A Page int `json:\"page\"`\u000A Fruits []string `json:\"fruits\"`\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' bolB, _ :\u003D json.Marshal(true)\u000A fmt.Println(string(bolB))\u000A');codeLines.push(' intB, _ :\u003D json.Marshal(1)\u000A fmt.Println(string(intB))\u000A');codeLines.push(' fltB, _ :\u003D json.Marshal(2.34)\u000A fmt.Println(string(fltB))\u000A');codeLines.push(' strB, _ :\u003D json.Marshal(\"gopher\")\u000A fmt.Println(string(strB))\u000A');codeLines.push(' slcD :\u003D []string{\"apple\", \"peach\", \"pear\"}\u000A slcB, _ :\u003D json.Marshal(slcD)\u000A fmt.Println(string(slcB))\u000A');codeLines.push(' mapD :\u003D map[string]int{\"apple\": 5, \"lettuce\": 7}\u000A mapB, _ :\u003D json.Marshal(mapD)\u000A fmt.Println(string(mapB))\u000A');codeLines.push(' res1D :\u003D \u0026response1{\u000A Page: 1,\u000A Fruits: []string{\"apple\", \"peach\", \"pear\"}}\u000A res1B, _ :\u003D json.Marshal(res1D)\u000A fmt.Println(string(res1B))\u000A');codeLines.push(' res2D :\u003D \u0026response2{\u000A Page: 1,\u000A Fruits: []string{\"apple\", \"peach\", \"pear\"}}\u000A res2B, _ :\u003D json.Marshal(res2D)\u000A fmt.Println(string(res2B))\u000A');codeLines.push(' byt :\u003D []byte(`{\"num\":6.13,\"strs\":[\"a\",\"b\"]}`)\u000A');codeLines.push(' var dat map[string]interface{}\u000A');codeLines.push(' if err :\u003D json.Unmarshal(byt, \u0026dat); err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(dat)\u000A');codeLines.push(' num :\u003D dat[\"num\"].(float64)\u000A fmt.Println(num)\u000A');codeLines.push(' strs :\u003D dat[\"strs\"].([]interface{})\u000A str1 :\u003D strs[0].(string)\u000A fmt.Println(str1)\u000A');codeLines.push(' str :\u003D `{\"page\": 1, \"fruits\": [\"apple\", \"peach\"]}`\u000A res :\u003D response2{}\u000A json.Unmarshal([]byte(str), \u0026res)\u000A fmt.Println(res)\u000A fmt.Println(res.Fruits[0])\u000A');codeLines.push(' enc :\u003D json.NewEncoder(os.Stdout)\u000A d :\u003D map[string]int{\"apple\": 5, \"lettuce\": 7}\u000A enc.Encode(d)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/line-filters generated
View File

@ -204,7 +204,7 @@ lowercase lines.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"os\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' scanner :\x3D bufio.NewScanner(os.Stdin)\u000A');codeLines.push(' for scanner.Scan() {\u000A');codeLines.push(' ucl :\x3D strings.ToUpper(scanner.Text())\u000A');codeLines.push(' fmt.Println(ucl)\u000A }\u000A');codeLines.push(' if err :\x3D scanner.Err(); err !\x3D nil {\u000A fmt.Fprintln(os.Stderr, \"error:\", err)\u000A os.Exit(1)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"os\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' scanner :\u003D bufio.NewScanner(os.Stdin)\u000A');codeLines.push(' for scanner.Scan() {\u000A');codeLines.push(' ucl :\u003D strings.ToUpper(scanner.Text())\u000A');codeLines.push(' fmt.Println(ucl)\u000A }\u000A');codeLines.push(' if err :\u003D scanner.Err(); err !\u003D nil {\u000A fmt.Fprintln(os.Stderr, \"error:\", err)\u000A os.Exit(1)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/maps generated
View File

@ -232,7 +232,7 @@ printed with <code>fmt.Println</code>.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' m :\x3D make(map[string]int)\u000A');codeLines.push(' m[\"k1\"] \x3D 7\u000A m[\"k2\"] \x3D 13\u000A');codeLines.push(' fmt.Println(\"map:\", m)\u000A');codeLines.push(' v1 :\x3D m[\"k1\"]\u000A fmt.Println(\"v1: \", v1)\u000A');codeLines.push(' fmt.Println(\"len:\", len(m))\u000A');codeLines.push(' delete(m, \"k2\")\u000A fmt.Println(\"map:\", m)\u000A');codeLines.push(' _, prs :\x3D m[\"k2\"]\u000A fmt.Println(\"prs:\", prs)\u000A');codeLines.push(' n :\x3D map[string]int{\"foo\": 1, \"bar\": 2}\u000A fmt.Println(\"map:\", n)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' m :\u003D make(map[string]int)\u000A');codeLines.push(' m[\"k1\"] \u003D 7\u000A m[\"k2\"] \u003D 13\u000A');codeLines.push(' fmt.Println(\"map:\", m)\u000A');codeLines.push(' v1 :\u003D m[\"k1\"]\u000A fmt.Println(\"v1: \", v1)\u000A');codeLines.push(' fmt.Println(\"len:\", len(m))\u000A');codeLines.push(' delete(m, \"k2\")\u000A fmt.Println(\"map:\", m)\u000A');codeLines.push(' _, prs :\u003D m[\"k2\"]\u000A fmt.Println(\"prs:\", prs)\u000A');codeLines.push(' n :\u003D map[string]int{\"foo\": 1, \"bar\": 2}\u000A fmt.Println(\"map:\", n)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/methods generated
View File

@ -197,7 +197,7 @@ naming related sets of methods: interfaces.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('type rect struct {\u000A width, height int\u000A}\u000A');codeLines.push('func (r *rect) area() int {\u000A return r.width * r.height\u000A}\u000A');codeLines.push('func (r rect) perim() int {\u000A return 2*r.width + 2*r.height\u000A}\u000A');codeLines.push('func main() {\u000A r :\x3D rect{width: 10, height: 5}\u000A');codeLines.push(' fmt.Println(\"area: \", r.area())\u000A fmt.Println(\"perim:\", r.perim())\u000A');codeLines.push(' rp :\x3D \x26r\u000A fmt.Println(\"area: \", rp.area())\u000A fmt.Println(\"perim:\", rp.perim())\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('type rect struct {\u000A width, height int\u000A}\u000A');codeLines.push('func (r *rect) area() int {\u000A return r.width * r.height\u000A}\u000A');codeLines.push('func (r rect) perim() int {\u000A return 2*r.width + 2*r.height\u000A}\u000A');codeLines.push('func main() {\u000A r :\u003D rect{width: 10, height: 5}\u000A');codeLines.push(' fmt.Println(\"area: \", r.area())\u000A fmt.Println(\"perim:\", r.perim())\u000A');codeLines.push(' rp :\u003D \u0026r\u000A fmt.Println(\"area: \", rp.area())\u000A fmt.Println(\"perim:\", rp.perim())\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -166,7 +166,7 @@ feature of Go functions; we&rsquo;ll look at this next.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func vals() (int, int) {\u000A return 3, 7\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' a, b :\x3D vals()\u000A fmt.Println(a)\u000A fmt.Println(b)\u000A');codeLines.push(' _, c :\x3D vals()\u000A fmt.Println(c)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func vals() (int, int) {\u000A return 3, 7\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' a, b :\u003D vals()\u000A fmt.Println(a)\u000A fmt.Println(b)\u000A');codeLines.push(' _, c :\u003D vals()\u000A fmt.Println(c)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/mutexes generated
View File

@ -297,7 +297,7 @@ management task using only goroutines and channels.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math/rand\"\u000A \"sync\"\u000A \"sync/atomic\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var state \x3D make(map[int]int)\u000A');codeLines.push(' var mutex \x3D \x26sync.Mutex{}\u000A');codeLines.push(' var readOps uint64\u000A var writeOps uint64\u000A');codeLines.push(' for r :\x3D 0; r \x3C 100; r++ {\u000A go func() {\u000A total :\x3D 0\u000A for {\u000A');codeLines.push(' key :\x3D rand.Intn(5)\u000A mutex.Lock()\u000A total +\x3D state[key]\u000A mutex.Unlock()\u000A atomic.AddUint64(\x26readOps, 1)\u000A');codeLines.push(' time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' for w :\x3D 0; w \x3C 10; w++ {\u000A go func() {\u000A for {\u000A key :\x3D rand.Intn(5)\u000A val :\x3D rand.Intn(100)\u000A mutex.Lock()\u000A state[key] \x3D val\u000A mutex.Unlock()\u000A atomic.AddUint64(\x26writeOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' time.Sleep(time.Second)\u000A');codeLines.push(' readOpsFinal :\x3D atomic.LoadUint64(\x26readOps)\u000A fmt.Println(\"readOps:\", readOpsFinal)\u000A writeOpsFinal :\x3D atomic.LoadUint64(\x26writeOps)\u000A fmt.Println(\"writeOps:\", writeOpsFinal)\u000A');codeLines.push(' mutex.Lock()\u000A fmt.Println(\"state:\", state)\u000A mutex.Unlock()\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math/rand\"\u000A \"sync\"\u000A \"sync/atomic\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var state \u003D make(map[int]int)\u000A');codeLines.push(' var mutex \u003D \u0026sync.Mutex{}\u000A');codeLines.push(' var readOps uint64\u000A var writeOps uint64\u000A');codeLines.push(' for r :\u003D 0; r \u003C 100; r++ {\u000A go func() {\u000A total :\u003D 0\u000A for {\u000A');codeLines.push(' key :\u003D rand.Intn(5)\u000A mutex.Lock()\u000A total +\u003D state[key]\u000A mutex.Unlock()\u000A atomic.AddUint64(\u0026readOps, 1)\u000A');codeLines.push(' time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' for w :\u003D 0; w \u003C 10; w++ {\u000A go func() {\u000A for {\u000A key :\u003D rand.Intn(5)\u000A val :\u003D rand.Intn(100)\u000A mutex.Lock()\u000A state[key] \u003D val\u000A mutex.Unlock()\u000A atomic.AddUint64(\u0026writeOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' time.Sleep(time.Second)\u000A');codeLines.push(' readOpsFinal :\u003D atomic.LoadUint64(\u0026readOps)\u000A fmt.Println(\"readOps:\", readOpsFinal)\u000A writeOpsFinal :\u003D atomic.LoadUint64(\u0026writeOps)\u000A fmt.Println(\"writeOps:\", writeOpsFinal)\u000A');codeLines.push(' mutex.Lock()\u000A fmt.Println(\"state:\", state)\u000A mutex.Unlock()\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -176,7 +176,7 @@ on both <code>messages</code> and <code>signals</code>.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A messages :\x3D make(chan string)\u000A signals :\x3D make(chan bool)\u000A');codeLines.push(' select {\u000A case msg :\x3D \x3C-messages:\u000A fmt.Println(\"received message\", msg)\u000A default:\u000A fmt.Println(\"no message received\")\u000A }\u000A');codeLines.push(' msg :\x3D \"hi\"\u000A select {\u000A case messages \x3C- msg:\u000A fmt.Println(\"sent message\", msg)\u000A default:\u000A fmt.Println(\"no message sent\")\u000A }\u000A');codeLines.push(' select {\u000A case msg :\x3D \x3C-messages:\u000A fmt.Println(\"received message\", msg)\u000A case sig :\x3D \x3C-signals:\u000A fmt.Println(\"received signal\", sig)\u000A default:\u000A fmt.Println(\"no activity\")\u000A }\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A messages :\u003D make(chan string)\u000A signals :\u003D make(chan bool)\u000A');codeLines.push(' select {\u000A case msg :\u003D \u003C-messages:\u000A fmt.Println(\"received message\", msg)\u000A default:\u000A fmt.Println(\"no message received\")\u000A }\u000A');codeLines.push(' msg :\u003D \"hi\"\u000A select {\u000A case messages \u003C- msg:\u000A fmt.Println(\"sent message\", msg)\u000A default:\u000A fmt.Println(\"no message sent\")\u000A }\u000A');codeLines.push(' select {\u000A case msg :\u003D \u003C-messages:\u000A fmt.Println(\"received message\", msg)\u000A case sig :\u003D \u003C-signals:\u000A fmt.Println(\"received signal\", sig)\u000A default:\u000A fmt.Println(\"no activity\")\u000A }\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/number-parsing generated
View File

@ -213,7 +213,7 @@ bits.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"strconv\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f, _ :\x3D strconv.ParseFloat(\"1.234\", 64)\u000A fmt.Println(f)\u000A');codeLines.push(' i, _ :\x3D strconv.ParseInt(\"123\", 0, 64)\u000A fmt.Println(i)\u000A');codeLines.push(' d, _ :\x3D strconv.ParseInt(\"0x1c8\", 0, 64)\u000A fmt.Println(d)\u000A');codeLines.push(' u, _ :\x3D strconv.ParseUint(\"789\", 0, 64)\u000A fmt.Println(u)\u000A');codeLines.push(' k, _ :\x3D strconv.Atoi(\"135\")\u000A fmt.Println(k)\u000A');codeLines.push(' _, e :\x3D strconv.Atoi(\"wat\")\u000A fmt.Println(e)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"strconv\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f, _ :\u003D strconv.ParseFloat(\"1.234\", 64)\u000A fmt.Println(f)\u000A');codeLines.push(' i, _ :\u003D strconv.ParseInt(\"123\", 0, 64)\u000A fmt.Println(i)\u000A');codeLines.push(' d, _ :\u003D strconv.ParseInt(\"0x1c8\", 0, 64)\u000A fmt.Println(d)\u000A');codeLines.push(' u, _ :\u003D strconv.ParseUint(\"789\", 0, 64)\u000A fmt.Println(u)\u000A');codeLines.push(' k, _ :\u003D strconv.Atoi(\"135\")\u000A fmt.Println(k)\u000A');codeLines.push(' _, e :\u003D strconv.Atoi(\"wat\")\u000A fmt.Println(e)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/panic generated
View File

@ -172,7 +172,7 @@ to use error-indicating return values wherever possible.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"os\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' panic(\"a problem\")\u000A');codeLines.push(' _, err :\x3D os.Create(\"/tmp/file\")\u000A if err !\x3D nil {\u000A panic(err)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"os\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' panic(\"a problem\")\u000A');codeLines.push(' _, err :\u003D os.Create(\"/tmp/file\")\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/pointers generated
View File

@ -193,7 +193,7 @@ the memory address for that variable.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func zeroval(ival int) {\u000A ival \x3D 0\u000A}\u000A');codeLines.push('func zeroptr(iptr *int) {\u000A *iptr \x3D 0\u000A}\u000A');codeLines.push('func main() {\u000A i :\x3D 1\u000A fmt.Println(\"initial:\", i)\u000A');codeLines.push(' zeroval(i)\u000A fmt.Println(\"zeroval:\", i)\u000A');codeLines.push(' zeroptr(\x26i)\u000A fmt.Println(\"zeroptr:\", i)\u000A');codeLines.push(' fmt.Println(\"pointer:\", \x26i)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func zeroval(ival int) {\u000A ival \u003D 0\u000A}\u000A');codeLines.push('func zeroptr(iptr *int) {\u000A *iptr \u003D 0\u000A}\u000A');codeLines.push('func main() {\u000A i :\u003D 1\u000A fmt.Println(\"initial:\", i)\u000A');codeLines.push(' zeroval(i)\u000A fmt.Println(\"zeroval:\", i)\u000A');codeLines.push(' zeroptr(\u0026i)\u000A fmt.Println(\"zeroptr:\", i)\u000A');codeLines.push(' fmt.Println(\"pointer:\", \u0026i)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/random-numbers generated
View File

@ -229,7 +229,7 @@ that Go can provide.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math/rand\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fmt.Print(rand.Intn(100), \",\")\u000A fmt.Print(rand.Intn(100))\u000A fmt.Println()\u000A');codeLines.push(' fmt.Println(rand.Float64())\u000A');codeLines.push(' fmt.Print((rand.Float64()*5)+5, \",\")\u000A fmt.Print((rand.Float64() * 5) + 5)\u000A fmt.Println()\u000A');codeLines.push(' s1 :\x3D rand.NewSource(time.Now().UnixNano())\u000A r1 :\x3D rand.New(s1)\u000A');codeLines.push(' fmt.Print(r1.Intn(100), \",\")\u000A fmt.Print(r1.Intn(100))\u000A fmt.Println()\u000A');codeLines.push(' s2 :\x3D rand.NewSource(42)\u000A r2 :\x3D rand.New(s2)\u000A fmt.Print(r2.Intn(100), \",\")\u000A fmt.Print(r2.Intn(100))\u000A fmt.Println()\u000A s3 :\x3D rand.NewSource(42)\u000A r3 :\x3D rand.New(s3)\u000A fmt.Print(r3.Intn(100), \",\")\u000A fmt.Print(r3.Intn(100))\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math/rand\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fmt.Print(rand.Intn(100), \",\")\u000A fmt.Print(rand.Intn(100))\u000A fmt.Println()\u000A');codeLines.push(' fmt.Println(rand.Float64())\u000A');codeLines.push(' fmt.Print((rand.Float64()*5)+5, \",\")\u000A fmt.Print((rand.Float64() * 5) + 5)\u000A fmt.Println()\u000A');codeLines.push(' s1 :\u003D rand.NewSource(time.Now().UnixNano())\u000A r1 :\u003D rand.New(s1)\u000A');codeLines.push(' fmt.Print(r1.Intn(100), \",\")\u000A fmt.Print(r1.Intn(100))\u000A fmt.Println()\u000A');codeLines.push(' s2 :\u003D rand.NewSource(42)\u000A r2 :\u003D rand.New(s2)\u000A fmt.Print(r2.Intn(100), \",\")\u000A fmt.Print(r2.Intn(100))\u000A fmt.Println()\u000A s3 :\u003D rand.NewSource(42)\u000A r3 :\u003D rand.New(s3)\u000A fmt.Print(r3.Intn(100), \",\")\u000A fmt.Print(r3.Intn(100))\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/range generated
View File

@ -200,7 +200,7 @@ of the <code>rune</code> and the second the <code>rune</code> itself.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' nums :\x3D []int{2, 3, 4}\u000A sum :\x3D 0\u000A for _, num :\x3D range nums {\u000A sum +\x3D num\u000A }\u000A fmt.Println(\"sum:\", sum)\u000A');codeLines.push(' for i, num :\x3D range nums {\u000A if num \x3D\x3D 3 {\u000A fmt.Println(\"index:\", i)\u000A }\u000A }\u000A');codeLines.push(' kvs :\x3D map[string]string{\"a\": \"apple\", \"b\": \"banana\"}\u000A for k, v :\x3D range kvs {\u000A fmt.Printf(\"%s -\x3E %s\\n\", k, v)\u000A }\u000A');codeLines.push(' for k :\x3D range kvs {\u000A fmt.Println(\"key:\", k)\u000A }\u000A');codeLines.push(' for i, c :\x3D range \"go\" {\u000A fmt.Println(i, c)\u000A }\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' nums :\u003D []int{2, 3, 4}\u000A sum :\u003D 0\u000A for _, num :\u003D range nums {\u000A sum +\u003D num\u000A }\u000A fmt.Println(\"sum:\", sum)\u000A');codeLines.push(' for i, num :\u003D range nums {\u000A if num \u003D\u003D 3 {\u000A fmt.Println(\"index:\", i)\u000A }\u000A }\u000A');codeLines.push(' kvs :\u003D map[string]string{\"a\": \"apple\", \"b\": \"banana\"}\u000A for k, v :\u003D range kvs {\u000A fmt.Printf(\"%s -\u003E %s\\n\", k, v)\u000A }\u000A');codeLines.push(' for k :\u003D range kvs {\u000A fmt.Println(\"key:\", k)\u000A }\u000A');codeLines.push(' for i, c :\u003D range \"go\" {\u000A fmt.Println(i, c)\u000A }\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -154,7 +154,7 @@ values be received.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' queue :\x3D make(chan string, 2)\u000A queue \x3C- \"one\"\u000A queue \x3C- \"two\"\u000A close(queue)\u000A');codeLines.push(' for elem :\x3D range queue {\u000A fmt.Println(elem)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' queue :\u003D make(chan string, 2)\u000A queue \u003C- \"one\"\u000A queue \u003C- \"two\"\u000A close(queue)\u000A');codeLines.push(' for elem :\u003D range queue {\u000A fmt.Println(elem)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/rate-limiting generated
View File

@ -261,7 +261,7 @@ then serve the remaining 2 with ~200ms delays each.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' requests :\x3D make(chan int, 5)\u000A for i :\x3D 1; i \x3C\x3D 5; i++ {\u000A requests \x3C- i\u000A }\u000A close(requests)\u000A');codeLines.push(' limiter :\x3D time.Tick(200 * time.Millisecond)\u000A');codeLines.push(' for req :\x3D range requests {\u000A \x3C-limiter\u000A fmt.Println(\"request\", req, time.Now())\u000A }\u000A');codeLines.push(' burstyLimiter :\x3D make(chan time.Time, 3)\u000A');codeLines.push(' for i :\x3D 0; i \x3C 3; i++ {\u000A burstyLimiter \x3C- time.Now()\u000A }\u000A');codeLines.push(' go func() {\u000A for t :\x3D range time.Tick(200 * time.Millisecond) {\u000A burstyLimiter \x3C- t\u000A }\u000A }()\u000A');codeLines.push(' burstyRequests :\x3D make(chan int, 5)\u000A for i :\x3D 1; i \x3C\x3D 5; i++ {\u000A burstyRequests \x3C- i\u000A }\u000A close(burstyRequests)\u000A for req :\x3D range burstyRequests {\u000A \x3C-burstyLimiter\u000A fmt.Println(\"request\", req, time.Now())\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' requests :\u003D make(chan int, 5)\u000A for i :\u003D 1; i \u003C\u003D 5; i++ {\u000A requests \u003C- i\u000A }\u000A close(requests)\u000A');codeLines.push(' limiter :\u003D time.Tick(200 * time.Millisecond)\u000A');codeLines.push(' for req :\u003D range requests {\u000A \u003C-limiter\u000A fmt.Println(\"request\", req, time.Now())\u000A }\u000A');codeLines.push(' burstyLimiter :\u003D make(chan time.Time, 3)\u000A');codeLines.push(' for i :\u003D 0; i \u003C 3; i++ {\u000A burstyLimiter \u003C- time.Now()\u000A }\u000A');codeLines.push(' go func() {\u000A for t :\u003D range time.Tick(200 * time.Millisecond) {\u000A burstyLimiter \u003C- t\u000A }\u000A }()\u000A');codeLines.push(' burstyRequests :\u003D make(chan int, 5)\u000A for i :\u003D 1; i \u003C\u003D 5; i++ {\u000A burstyRequests \u003C- i\u000A }\u000A close(burstyRequests)\u000A for req :\u003D range burstyRequests {\u000A \u003C-burstyLimiter\u000A fmt.Println(\"request\", req, time.Now())\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/reading-files generated
View File

@ -287,7 +287,7 @@ be scheduled immediately after <code>Open</code>ing with
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"io\"\u000A \"io/ioutil\"\u000A \"os\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\x3D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' dat, err :\x3D ioutil.ReadFile(\"/tmp/dat\")\u000A check(err)\u000A fmt.Print(string(dat))\u000A');codeLines.push(' f, err :\x3D os.Open(\"/tmp/dat\")\u000A check(err)\u000A');codeLines.push(' b1 :\x3D make([]byte, 5)\u000A n1, err :\x3D f.Read(b1)\u000A check(err)\u000A fmt.Printf(\"%d bytes: %s\\n\", n1, string(b1[:n1]))\u000A');codeLines.push(' o2, err :\x3D f.Seek(6, 0)\u000A check(err)\u000A b2 :\x3D make([]byte, 2)\u000A n2, err :\x3D f.Read(b2)\u000A check(err)\u000A fmt.Printf(\"%d bytes @ %d: \", n2, o2)\u000A fmt.Printf(\"%v\\n\", string(b2[:n2]))\u000A');codeLines.push(' o3, err :\x3D f.Seek(6, 0)\u000A check(err)\u000A b3 :\x3D make([]byte, 2)\u000A n3, err :\x3D io.ReadAtLeast(f, b3, 2)\u000A check(err)\u000A fmt.Printf(\"%d bytes @ %d: %s\\n\", n3, o3, string(b3))\u000A');codeLines.push(' _, err \x3D f.Seek(0, 0)\u000A check(err)\u000A');codeLines.push(' r4 :\x3D bufio.NewReader(f)\u000A b4, err :\x3D r4.Peek(5)\u000A check(err)\u000A fmt.Printf(\"5 bytes: %s\\n\", string(b4))\u000A');codeLines.push(' f.Close()\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"io\"\u000A \"io/ioutil\"\u000A \"os\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\u003D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' dat, err :\u003D ioutil.ReadFile(\"/tmp/dat\")\u000A check(err)\u000A fmt.Print(string(dat))\u000A');codeLines.push(' f, err :\u003D os.Open(\"/tmp/dat\")\u000A check(err)\u000A');codeLines.push(' b1 :\u003D make([]byte, 5)\u000A n1, err :\u003D f.Read(b1)\u000A check(err)\u000A fmt.Printf(\"%d bytes: %s\\n\", n1, string(b1[:n1]))\u000A');codeLines.push(' o2, err :\u003D f.Seek(6, 0)\u000A check(err)\u000A b2 :\u003D make([]byte, 2)\u000A n2, err :\u003D f.Read(b2)\u000A check(err)\u000A fmt.Printf(\"%d bytes @ %d: \", n2, o2)\u000A fmt.Printf(\"%v\\n\", string(b2[:n2]))\u000A');codeLines.push(' o3, err :\u003D f.Seek(6, 0)\u000A check(err)\u000A b3 :\u003D make([]byte, 2)\u000A n3, err :\u003D io.ReadAtLeast(f, b3, 2)\u000A check(err)\u000A fmt.Printf(\"%d bytes @ %d: %s\\n\", n3, o3, string(b3))\u000A');codeLines.push(' _, err \u003D f.Seek(0, 0)\u000A check(err)\u000A');codeLines.push(' r4 :\u003D bufio.NewReader(f)\u000A b4, err :\u003D r4.Peek(5)\u000A check(err)\u000A fmt.Printf(\"5 bytes: %s\\n\", string(b4))\u000A');codeLines.push(' f.Close()\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

284
public/recover generated Normal file
View File

@ -0,0 +1,284 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Recover</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'defer';
}
if (e.key == "ArrowRight") {
window.location.href = 'collection-functions';
}
}
</script>
<body>
<div class="example" id="recover">
<h2><a href="./">Go by Example</a>: Recover</h2>
<table>
<tr>
<td class="docs">
<p>A <code>recover</code> means recovering from a <code>panic</code>, either from a &ldquo;business&rdquo; or &ldquo;built-in&rdquo; panic.\n
We want to recover if we want to handle a panic, stopping it from propagating upwards.</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="http://play.golang.org/p/uVo4c0IE97q"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
<span class="s">&quot;fmt&quot;</span>
<span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">recoverFromBuiltInPanic</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">()</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">recoverFromCustomPanic</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>defer is defined.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">recoverFromBuiltInPanic</span><span class="p">(</span><span class="nx">i</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="k">defer</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">r</span> <span class="o">:=</span> <span class="nb">recover</span><span class="p">();</span> <span class="nx">r</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;Recovered. Error:\n&quot;</span><span class="p">,</span> <span class="nx">r</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}()</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="kd">var</span> <span class="nx">a</span> <span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;Getting index %d&quot;</span><span class="o">+</span>
<span class="s">&quot; of array of len %d...\n&quot;</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nx">a</span><span class="p">))</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;Item in index %d: %d&quot;</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">a</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>defer is defined.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">recoverFromCustomPanic</span><span class="p">(</span><span class="nx">i</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="k">defer</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">r</span> <span class="o">:=</span> <span class="nb">recover</span><span class="p">();</span> <span class="nx">r</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;Recovered. Error:\n&quot;</span><span class="p">,</span> <span class="nx">r</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}()</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;About to process i=%d\n&quot;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="k">if</span> <span class="nx">i</span> <span class="p">&lt;</span> <span class="mi">0</span> <span class="p">{</span>
<span class="nb">panic</span><span class="p">(</span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">&quot;Accepting only&quot;</span><span class="o">+</span>
<span class="s">&quot; non-negative numbers but received %d&quot;</span><span class="p">,</span> <span class="nx">i</span><span class="p">))</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;Doing something with %d\n&quot;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>Running this program will exit correctly,
even though panic was invoked in two methods.
The recover is responsible for recovering from panics.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> go run recover.go
<span class="go">Getting index 10 of array of len 5...</span>
<span class="go">Recovered. Error:</span>
<span class="go"> runtime error: index out of range [10] with length 5</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Note that, in Go it is idiomatic
to use error-indicating return values wherever possible.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="go">About to process i=-1</span>
<span class="go">Recovered. Error:</span>
<span class="go"> Accepting only non-negative numbers but received -1</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="collection-functions">Collection Functions</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/recover">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' recoverFromBuiltInPanic(10)\u000A');codeLines.push(' fmt.Println()\u000A');codeLines.push(' recoverFromCustomPanic(-1)\u000A}\u000A');codeLines.push('func recoverFromBuiltInPanic(i int) {\u000A');codeLines.push(' defer func() {\u000A if r :\u003D recover(); r !\u003D nil {\u000A fmt.Println(\"Recovered. Error:\\n\", r)\u000A }\u000A }()\u000A');codeLines.push(' var a [5]int\u000A fmt.Printf(\"Getting index %d\"+\u000A \" of array of len %d...\\n\", i, len(a))\u000A fmt.Printf(\"Item in index %d: %d\", i, a[i])\u000A}\u000A');codeLines.push('func recoverFromCustomPanic(i int) {\u000A');codeLines.push(' defer func() {\u000A if r :\u003D recover(); r !\u003D nil {\u000A fmt.Println(\"Recovered. Error:\\n\", r)\u000A }\u000A }()\u000A');codeLines.push(' fmt.Printf(\"About to process i\u003D%d\\n\", i)\u000A');codeLines.push(' if i \u003C 0 {\u000A panic(fmt.Errorf(\"Accepting only\"+\u000A \" non-negative numbers but received %d\", i))\u000A }\u000A');codeLines.push(' fmt.Printf(\"Doing something with %d\\n\", i)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>

2
public/recursion generated
View File

@ -125,7 +125,7 @@ base case of <code>fact(0)</code>.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func fact(n int) int {\u000A if n \x3D\x3D 0 {\u000A return 1\u000A }\u000A return n * fact(n-1)\u000A}\u000A');codeLines.push('func main() {\u000A fmt.Println(fact(7))\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func fact(n int) int {\u000A if n \u003D\u003D 0 {\u000A return 1\u000A }\u000A return n * fact(n-1)\u000A}\u000A');codeLines.push('func main() {\u000A fmt.Println(fact(7))\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -343,7 +343,7 @@ the <a href="http://golang.org/pkg/regexp/"><code>regexp</code></a> package docs
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bytes\"\u000A \"fmt\"\u000A \"regexp\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' match, _ :\x3D regexp.MatchString(\"p([a-z]+)ch\", \"peach\")\u000A fmt.Println(match)\u000A');codeLines.push(' r, _ :\x3D regexp.Compile(\"p([a-z]+)ch\")\u000A');codeLines.push(' fmt.Println(r.MatchString(\"peach\"))\u000A');codeLines.push(' fmt.Println(r.FindString(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindStringIndex(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindStringSubmatch(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindStringSubmatchIndex(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindAllString(\"peach punch pinch\", -1))\u000A');codeLines.push(' fmt.Println(r.FindAllStringSubmatchIndex(\u000A \"peach punch pinch\", -1))\u000A');codeLines.push(' fmt.Println(r.FindAllString(\"peach punch pinch\", 2))\u000A');codeLines.push(' fmt.Println(r.Match([]byte(\"peach\")))\u000A');codeLines.push(' r \x3D regexp.MustCompile(\"p([a-z]+)ch\")\u000A fmt.Println(r)\u000A');codeLines.push(' fmt.Println(r.ReplaceAllString(\"a peach\", \"\x3Cfruit\x3E\"))\u000A');codeLines.push(' in :\x3D []byte(\"a peach\")\u000A out :\x3D r.ReplaceAllFunc(in, bytes.ToUpper)\u000A fmt.Println(string(out))\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bytes\"\u000A \"fmt\"\u000A \"regexp\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' match, _ :\u003D regexp.MatchString(\"p([a-z]+)ch\", \"peach\")\u000A fmt.Println(match)\u000A');codeLines.push(' r, _ :\u003D regexp.Compile(\"p([a-z]+)ch\")\u000A');codeLines.push(' fmt.Println(r.MatchString(\"peach\"))\u000A');codeLines.push(' fmt.Println(r.FindString(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindStringIndex(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindStringSubmatch(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindStringSubmatchIndex(\"peach punch\"))\u000A');codeLines.push(' fmt.Println(r.FindAllString(\"peach punch pinch\", -1))\u000A');codeLines.push(' fmt.Println(r.FindAllStringSubmatchIndex(\u000A \"peach punch pinch\", -1))\u000A');codeLines.push(' fmt.Println(r.FindAllString(\"peach punch pinch\", 2))\u000A');codeLines.push(' fmt.Println(r.Match([]byte(\"peach\")))\u000A');codeLines.push(' r \u003D regexp.MustCompile(\"p([a-z]+)ch\")\u000A fmt.Println(r)\u000A');codeLines.push(' fmt.Println(r.ReplaceAllString(\"a peach\", \"\u003Cfruit\u003E\"))\u000A');codeLines.push(' in :\u003D []byte(\"a peach\")\u000A out :\u003D r.ReplaceAllFunc(in, bytes.ToUpper)\u000A fmt.Println(string(out))\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/select generated
View File

@ -183,7 +183,7 @@ concurrently.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' c1 :\x3D make(chan string)\u000A c2 :\x3D make(chan string)\u000A');codeLines.push(' go func() {\u000A time.Sleep(1 * time.Second)\u000A c1 \x3C- \"one\"\u000A }()\u000A go func() {\u000A time.Sleep(2 * time.Second)\u000A c2 \x3C- \"two\"\u000A }()\u000A');codeLines.push(' for i :\x3D 0; i \x3C 2; i++ {\u000A select {\u000A case msg1 :\x3D \x3C-c1:\u000A fmt.Println(\"received\", msg1)\u000A case msg2 :\x3D \x3C-c2:\u000A fmt.Println(\"received\", msg2)\u000A }\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' c1 :\u003D make(chan string)\u000A c2 :\u003D make(chan string)\u000A');codeLines.push(' go func() {\u000A time.Sleep(1 * time.Second)\u000A c1 \u003C- \"one\"\u000A }()\u000A go func() {\u000A time.Sleep(2 * time.Second)\u000A c2 \u003C- \"two\"\u000A }()\u000A');codeLines.push(' for i :\u003D 0; i \u003C 2; i++ {\u000A select {\u000A case msg1 :\u003D \u003C-c1:\u000A fmt.Println(\"received\", msg1)\u000A case msg2 :\u003D \u003C-c2:\u000A fmt.Println(\"received\", msg2)\u000A }\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/sha1-hashes generated
View File

@ -203,7 +203,7 @@ you should carefully research
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"crypto/sha1\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A s :\x3D \"sha1 this string\"\u000A');codeLines.push(' h :\x3D sha1.New()\u000A');codeLines.push(' h.Write([]byte(s))\u000A');codeLines.push(' bs :\x3D h.Sum(nil)\u000A');codeLines.push(' fmt.Println(s)\u000A fmt.Printf(\"%x\\n\", bs)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"crypto/sha1\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A s :\u003D \"sha1 this string\"\u000A');codeLines.push(' h :\u003D sha1.New()\u000A');codeLines.push(' h.Write([]byte(s))\u000A');codeLines.push(' bs :\u003D h.Sum(nil)\u000A');codeLines.push(' fmt.Println(s)\u000A fmt.Printf(\"%x\\n\", bs)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/signals generated
View File

@ -188,7 +188,7 @@ causing the program to print <code>interrupt</code> and then exit.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A \"os/signal\"\u000A \"syscall\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' sigs :\x3D make(chan os.Signal, 1)\u000A done :\x3D make(chan bool, 1)\u000A');codeLines.push(' signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)\u000A');codeLines.push(' go func() {\u000A sig :\x3D \x3C-sigs\u000A fmt.Println()\u000A fmt.Println(sig)\u000A done \x3C- true\u000A }()\u000A');codeLines.push(' fmt.Println(\"awaiting signal\")\u000A \x3C-done\u000A fmt.Println(\"exiting\")\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A \"os/signal\"\u000A \"syscall\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' sigs :\u003D make(chan os.Signal, 1)\u000A done :\u003D make(chan bool, 1)\u000A');codeLines.push(' signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)\u000A');codeLines.push(' go func() {\u000A sig :\u003D \u003C-sigs\u000A fmt.Println()\u000A fmt.Println(sig)\u000A done \u003C- true\u000A }()\u000A');codeLines.push(' fmt.Println(\"awaiting signal\")\u000A \u003C-done\u000A fmt.Println(\"exiting\")\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/slices generated
View File

@ -308,7 +308,7 @@ Go&rsquo;s other key builtin data structure: maps.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' s :\x3D make([]string, 3)\u000A fmt.Println(\"emp:\", s)\u000A');codeLines.push(' s[0] \x3D \"a\"\u000A s[1] \x3D \"b\"\u000A s[2] \x3D \"c\"\u000A fmt.Println(\"set:\", s)\u000A fmt.Println(\"get:\", s[2])\u000A');codeLines.push(' fmt.Println(\"len:\", len(s))\u000A');codeLines.push(' s \x3D append(s, \"d\")\u000A s \x3D append(s, \"e\", \"f\")\u000A fmt.Println(\"apd:\", s)\u000A');codeLines.push(' c :\x3D make([]string, len(s))\u000A copy(c, s)\u000A fmt.Println(\"cpy:\", c)\u000A');codeLines.push(' l :\x3D s[2:5]\u000A fmt.Println(\"sl1:\", l)\u000A');codeLines.push(' l \x3D s[:5]\u000A fmt.Println(\"sl2:\", l)\u000A');codeLines.push(' l \x3D s[2:]\u000A fmt.Println(\"sl3:\", l)\u000A');codeLines.push(' t :\x3D []string{\"g\", \"h\", \"i\"}\u000A fmt.Println(\"dcl:\", t)\u000A');codeLines.push(' twoD :\x3D make([][]int, 3)\u000A for i :\x3D 0; i \x3C 3; i++ {\u000A innerLen :\x3D i + 1\u000A twoD[i] \x3D make([]int, innerLen)\u000A for j :\x3D 0; j \x3C innerLen; j++ {\u000A twoD[i][j] \x3D i + j\u000A }\u000A }\u000A fmt.Println(\"2d: \", twoD)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' s :\u003D make([]string, 3)\u000A fmt.Println(\"emp:\", s)\u000A');codeLines.push(' s[0] \u003D \"a\"\u000A s[1] \u003D \"b\"\u000A s[2] \u003D \"c\"\u000A fmt.Println(\"set:\", s)\u000A fmt.Println(\"get:\", s[2])\u000A');codeLines.push(' fmt.Println(\"len:\", len(s))\u000A');codeLines.push(' s \u003D append(s, \"d\")\u000A s \u003D append(s, \"e\", \"f\")\u000A fmt.Println(\"apd:\", s)\u000A');codeLines.push(' c :\u003D make([]string, len(s))\u000A copy(c, s)\u000A fmt.Println(\"cpy:\", c)\u000A');codeLines.push(' l :\u003D s[2:5]\u000A fmt.Println(\"sl1:\", l)\u000A');codeLines.push(' l \u003D s[:5]\u000A fmt.Println(\"sl2:\", l)\u000A');codeLines.push(' l \u003D s[2:]\u000A fmt.Println(\"sl3:\", l)\u000A');codeLines.push(' t :\u003D []string{\"g\", \"h\", \"i\"}\u000A fmt.Println(\"dcl:\", t)\u000A');codeLines.push(' twoD :\u003D make([][]int, 3)\u000A for i :\u003D 0; i \u003C 3; i++ {\u000A innerLen :\u003D i + 1\u000A twoD[i] \u003D make([]int, innerLen)\u000A for j :\u003D 0; j \u003C innerLen; j++ {\u000A twoD[i][j] \u003D i + j\u000A }\u000A }\u000A fmt.Println(\"2d: \", twoD)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/sorting generated
View File

@ -160,7 +160,7 @@ slices and <code>true</code> as the result of our <code>AreSorted</code> test.</
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sort\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' strs :\x3D []string{\"c\", \"a\", \"b\"}\u000A sort.Strings(strs)\u000A fmt.Println(\"Strings:\", strs)\u000A');codeLines.push(' ints :\x3D []int{7, 2, 4}\u000A sort.Ints(ints)\u000A fmt.Println(\"Ints: \", ints)\u000A');codeLines.push(' s :\x3D sort.IntsAreSorted(ints)\u000A fmt.Println(\"Sorted: \", s)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sort\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' strs :\u003D []string{\"c\", \"a\", \"b\"}\u000A sort.Strings(strs)\u000A fmt.Println(\"Strings:\", strs)\u000A');codeLines.push(' ints :\u003D []int{7, 2, 4}\u000A sort.Ints(ints)\u000A fmt.Println(\"Ints: \", ints)\u000A');codeLines.push(' s :\u003D sort.IntsAreSorted(ints)\u000A fmt.Println(\"Sorted: \", s)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -177,7 +177,7 @@ functions.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sort\"\u000A)\u000A');codeLines.push('type byLength []string\u000A');codeLines.push('func (s byLength) Len() int {\u000A return len(s)\u000A}\u000Afunc (s byLength) Swap(i, j int) {\u000A s[i], s[j] \x3D s[j], s[i]\u000A}\u000Afunc (s byLength) Less(i, j int) bool {\u000A return len(s[i]) \x3C len(s[j])\u000A}\u000A');codeLines.push('func main() {\u000A fruits :\x3D []string{\"peach\", \"banana\", \"kiwi\"}\u000A sort.Sort(byLength(fruits))\u000A fmt.Println(fruits)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sort\"\u000A)\u000A');codeLines.push('type byLength []string\u000A');codeLines.push('func (s byLength) Len() int {\u000A return len(s)\u000A}\u000Afunc (s byLength) Swap(i, j int) {\u000A s[i], s[j] \u003D s[j], s[i]\u000A}\u000Afunc (s byLength) Less(i, j int) bool {\u000A return len(s[i]) \u003C len(s[j])\u000A}\u000A');codeLines.push('func main() {\u000A fruits :\u003D []string{\"peach\", \"banana\", \"kiwi\"}\u000A sort.Sort(byLength(fruits))\u000A fmt.Println(fruits)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -259,7 +259,7 @@ as if we had run them directly from the command-line.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"io/ioutil\"\u000A \"os/exec\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' dateCmd :\x3D exec.Command(\"date\")\u000A');codeLines.push(' dateOut, err :\x3D dateCmd.Output()\u000A if err !\x3D nil {\u000A panic(err)\u000A }\u000A fmt.Println(\"\x3E date\")\u000A fmt.Println(string(dateOut))\u000A');codeLines.push(' grepCmd :\x3D exec.Command(\"grep\", \"hello\")\u000A');codeLines.push(' grepIn, _ :\x3D grepCmd.StdinPipe()\u000A grepOut, _ :\x3D grepCmd.StdoutPipe()\u000A grepCmd.Start()\u000A grepIn.Write([]byte(\"hello grep\\ngoodbye grep\"))\u000A grepIn.Close()\u000A grepBytes, _ :\x3D ioutil.ReadAll(grepOut)\u000A grepCmd.Wait()\u000A');codeLines.push(' fmt.Println(\"\x3E grep hello\")\u000A fmt.Println(string(grepBytes))\u000A');codeLines.push(' lsCmd :\x3D exec.Command(\"bash\", \"-c\", \"ls -a -l -h\")\u000A lsOut, err :\x3D lsCmd.Output()\u000A if err !\x3D nil {\u000A panic(err)\u000A }\u000A fmt.Println(\"\x3E ls -a -l -h\")\u000A fmt.Println(string(lsOut))\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"io/ioutil\"\u000A \"os/exec\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' dateCmd :\u003D exec.Command(\"date\")\u000A');codeLines.push(' dateOut, err :\u003D dateCmd.Output()\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(\"\u003E date\")\u000A fmt.Println(string(dateOut))\u000A');codeLines.push(' grepCmd :\u003D exec.Command(\"grep\", \"hello\")\u000A');codeLines.push(' grepIn, _ :\u003D grepCmd.StdinPipe()\u000A grepOut, _ :\u003D grepCmd.StdoutPipe()\u000A grepCmd.Start()\u000A grepIn.Write([]byte(\"hello grep\\ngoodbye grep\"))\u000A grepIn.Close()\u000A grepBytes, _ :\u003D ioutil.ReadAll(grepOut)\u000A grepCmd.Wait()\u000A');codeLines.push(' fmt.Println(\"\u003E grep hello\")\u000A fmt.Println(string(grepBytes))\u000A');codeLines.push(' lsCmd :\u003D exec.Command(\"bash\", \"-c\", \"ls -a -l -h\")\u000A lsOut, err :\u003D lsCmd.Output()\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(\"\u003E ls -a -l -h\")\u000A fmt.Println(string(lsOut))\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -312,7 +312,7 @@ program.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math/rand\"\u000A \"sync/atomic\"\u000A \"time\"\u000A)\u000A');codeLines.push('type readOp struct {\u000A key int\u000A resp chan int\u000A}\u000Atype writeOp struct {\u000A key int\u000A val int\u000A resp chan bool\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var readOps uint64\u000A var writeOps uint64\u000A');codeLines.push(' reads :\x3D make(chan readOp)\u000A writes :\x3D make(chan writeOp)\u000A');codeLines.push(' go func() {\u000A var state \x3D make(map[int]int)\u000A for {\u000A select {\u000A case read :\x3D \x3C-reads:\u000A read.resp \x3C- state[read.key]\u000A case write :\x3D \x3C-writes:\u000A state[write.key] \x3D write.val\u000A write.resp \x3C- true\u000A }\u000A }\u000A }()\u000A');codeLines.push(' for r :\x3D 0; r \x3C 100; r++ {\u000A go func() {\u000A for {\u000A read :\x3D readOp{\u000A key: rand.Intn(5),\u000A resp: make(chan int)}\u000A reads \x3C- read\u000A \x3C-read.resp\u000A atomic.AddUint64(\x26readOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' for w :\x3D 0; w \x3C 10; w++ {\u000A go func() {\u000A for {\u000A write :\x3D writeOp{\u000A key: rand.Intn(5),\u000A val: rand.Intn(100),\u000A resp: make(chan bool)}\u000A writes \x3C- write\u000A \x3C-write.resp\u000A atomic.AddUint64(\x26writeOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' time.Sleep(time.Second)\u000A');codeLines.push(' readOpsFinal :\x3D atomic.LoadUint64(\x26readOps)\u000A fmt.Println(\"readOps:\", readOpsFinal)\u000A writeOpsFinal :\x3D atomic.LoadUint64(\x26writeOps)\u000A fmt.Println(\"writeOps:\", writeOpsFinal)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math/rand\"\u000A \"sync/atomic\"\u000A \"time\"\u000A)\u000A');codeLines.push('type readOp struct {\u000A key int\u000A resp chan int\u000A}\u000Atype writeOp struct {\u000A key int\u000A val int\u000A resp chan bool\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var readOps uint64\u000A var writeOps uint64\u000A');codeLines.push(' reads :\u003D make(chan readOp)\u000A writes :\u003D make(chan writeOp)\u000A');codeLines.push(' go func() {\u000A var state \u003D make(map[int]int)\u000A for {\u000A select {\u000A case read :\u003D \u003C-reads:\u000A read.resp \u003C- state[read.key]\u000A case write :\u003D \u003C-writes:\u000A state[write.key] \u003D write.val\u000A write.resp \u003C- true\u000A }\u000A }\u000A }()\u000A');codeLines.push(' for r :\u003D 0; r \u003C 100; r++ {\u000A go func() {\u000A for {\u000A read :\u003D readOp{\u000A key: rand.Intn(5),\u000A resp: make(chan int)}\u000A reads \u003C- read\u000A \u003C-read.resp\u000A atomic.AddUint64(\u0026readOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' for w :\u003D 0; w \u003C 10; w++ {\u000A go func() {\u000A for {\u000A write :\u003D writeOp{\u000A key: rand.Intn(5),\u000A val: rand.Intn(100),\u000A resp: make(chan bool)}\u000A writes \u003C- write\u000A \u003C-write.resp\u000A atomic.AddUint64(\u0026writeOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' time.Sleep(time.Second)\u000A');codeLines.push(' readOpsFinal :\u003D atomic.LoadUint64(\u0026readOps)\u000A fmt.Println(\"readOps:\", readOpsFinal)\u000A writeOpsFinal :\u003D atomic.LoadUint64(\u0026writeOps)\u000A fmt.Println(\"writeOps:\", writeOpsFinal)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -457,7 +457,7 @@ and returns a string without printing it anywhere.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('type point struct {\u000A x, y int\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p :\x3D point{1, 2}\u000A fmt.Printf(\"%v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"%+v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"%#v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"%T\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"%t\\n\", true)\u000A');codeLines.push(' fmt.Printf(\"%d\\n\", 123)\u000A');codeLines.push(' fmt.Printf(\"%b\\n\", 14)\u000A');codeLines.push(' fmt.Printf(\"%c\\n\", 33)\u000A');codeLines.push(' fmt.Printf(\"%x\\n\", 456)\u000A');codeLines.push(' fmt.Printf(\"%f\\n\", 78.9)\u000A');codeLines.push(' fmt.Printf(\"%e\\n\", 123400000.0)\u000A fmt.Printf(\"%E\\n\", 123400000.0)\u000A');codeLines.push(' fmt.Printf(\"%s\\n\", \"\\\"string\\\"\")\u000A');codeLines.push(' fmt.Printf(\"%q\\n\", \"\\\"string\\\"\")\u000A');codeLines.push(' fmt.Printf(\"%x\\n\", \"hex this\")\u000A');codeLines.push(' fmt.Printf(\"%p\\n\", \x26p)\u000A');codeLines.push(' fmt.Printf(\"|%6d|%6d|\\n\", 12, 345)\u000A');codeLines.push(' fmt.Printf(\"|%6.2f|%6.2f|\\n\", 1.2, 3.45)\u000A');codeLines.push(' fmt.Printf(\"|%-6.2f|%-6.2f|\\n\", 1.2, 3.45)\u000A');codeLines.push(' fmt.Printf(\"|%6s|%6s|\\n\", \"foo\", \"b\")\u000A');codeLines.push(' fmt.Printf(\"|%-6s|%-6s|\\n\", \"foo\", \"b\")\u000A');codeLines.push(' s :\x3D fmt.Sprintf(\"a %s\", \"string\")\u000A fmt.Println(s)\u000A');codeLines.push(' fmt.Fprintf(os.Stderr, \"an %s\\n\", \"error\")\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('type point struct {\u000A x, y int\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p :\u003D point{1, 2}\u000A fmt.Printf(\"%v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"%+v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"%#v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"%T\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"%t\\n\", true)\u000A');codeLines.push(' fmt.Printf(\"%d\\n\", 123)\u000A');codeLines.push(' fmt.Printf(\"%b\\n\", 14)\u000A');codeLines.push(' fmt.Printf(\"%c\\n\", 33)\u000A');codeLines.push(' fmt.Printf(\"%x\\n\", 456)\u000A');codeLines.push(' fmt.Printf(\"%f\\n\", 78.9)\u000A');codeLines.push(' fmt.Printf(\"%e\\n\", 123400000.0)\u000A fmt.Printf(\"%E\\n\", 123400000.0)\u000A');codeLines.push(' fmt.Printf(\"%s\\n\", \"\\\"string\\\"\")\u000A');codeLines.push(' fmt.Printf(\"%q\\n\", \"\\\"string\\\"\")\u000A');codeLines.push(' fmt.Printf(\"%x\\n\", \"hex this\")\u000A');codeLines.push(' fmt.Printf(\"%p\\n\", \u0026p)\u000A');codeLines.push(' fmt.Printf(\"|%6d|%6d|\\n\", 12, 345)\u000A');codeLines.push(' fmt.Printf(\"|%6.2f|%6.2f|\\n\", 1.2, 3.45)\u000A');codeLines.push(' fmt.Printf(\"|%-6.2f|%-6.2f|\\n\", 1.2, 3.45)\u000A');codeLines.push(' fmt.Printf(\"|%6s|%6s|\\n\", \"foo\", \"b\")\u000A');codeLines.push(' fmt.Printf(\"|%-6s|%-6s|\\n\", \"foo\", \"b\")\u000A');codeLines.push(' s :\u003D fmt.Sprintf(\"a %s\", \"string\")\u000A fmt.Println(s)\u000A');codeLines.push(' fmt.Fprintf(os.Stderr, \"an %s\\n\", \"error\")\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -209,7 +209,7 @@ for more information.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A s \"strings\"\u000A)\u000A');codeLines.push('var p \x3D fmt.Println\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p(\"Contains: \", s.Contains(\"test\", \"es\"))\u000A p(\"Count: \", s.Count(\"test\", \"t\"))\u000A p(\"HasPrefix: \", s.HasPrefix(\"test\", \"te\"))\u000A p(\"HasSuffix: \", s.HasSuffix(\"test\", \"st\"))\u000A p(\"Index: \", s.Index(\"test\", \"e\"))\u000A p(\"Join: \", s.Join([]string{\"a\", \"b\"}, \"-\"))\u000A p(\"Repeat: \", s.Repeat(\"a\", 5))\u000A p(\"Replace: \", s.Replace(\"foo\", \"o\", \"0\", -1))\u000A p(\"Replace: \", s.Replace(\"foo\", \"o\", \"0\", 1))\u000A p(\"Split: \", s.Split(\"a-b-c-d-e\", \"-\"))\u000A p(\"ToLower: \", s.ToLower(\"TEST\"))\u000A p(\"ToUpper: \", s.ToUpper(\"test\"))\u000A p()\u000A');codeLines.push(' p(\"Len: \", len(\"hello\"))\u000A p(\"Char:\", \"hello\"[1])\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A s \"strings\"\u000A)\u000A');codeLines.push('var p \u003D fmt.Println\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p(\"Contains: \", s.Contains(\"test\", \"es\"))\u000A p(\"Count: \", s.Count(\"test\", \"t\"))\u000A p(\"HasPrefix: \", s.HasPrefix(\"test\", \"te\"))\u000A p(\"HasSuffix: \", s.HasSuffix(\"test\", \"st\"))\u000A p(\"Index: \", s.Index(\"test\", \"e\"))\u000A p(\"Join: \", s.Join([]string{\"a\", \"b\"}, \"-\"))\u000A p(\"Repeat: \", s.Repeat(\"a\", 5))\u000A p(\"Replace: \", s.Replace(\"foo\", \"o\", \"0\", -1))\u000A p(\"Replace: \", s.Replace(\"foo\", \"o\", \"0\", 1))\u000A p(\"Split: \", s.Split(\"a-b-c-d-e\", \"-\"))\u000A p(\"ToLower: \", s.ToLower(\"TEST\"))\u000A p(\"ToUpper: \", s.ToUpper(\"test\"))\u000A p()\u000A');codeLines.push(' p(\"Len: \", len(\"hello\"))\u000A p(\"Char:\", \"hello\"[1])\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/structs generated
View File

@ -266,7 +266,7 @@ pointers are automatically dereferenced.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('type person struct {\u000A name string\u000A age int\u000A}\u000A');codeLines.push('func newPerson(name string) *person {\u000A');codeLines.push(' p :\x3D person{name: name}\u000A p.age \x3D 42\u000A return \x26p\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fmt.Println(person{\"Bob\", 20})\u000A');codeLines.push(' fmt.Println(person{name: \"Alice\", age: 30})\u000A');codeLines.push(' fmt.Println(person{name: \"Fred\"})\u000A');codeLines.push(' fmt.Println(\x26person{name: \"Ann\", age: 40})\u000A');codeLines.push(' fmt.Println(newPerson(\"Jon\"))\u000A');codeLines.push(' s :\x3D person{name: \"Sean\", age: 50}\u000A fmt.Println(s.name)\u000A');codeLines.push(' sp :\x3D \x26s\u000A fmt.Println(sp.age)\u000A');codeLines.push(' sp.age \x3D 51\u000A fmt.Println(sp.age)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('type person struct {\u000A name string\u000A age int\u000A}\u000A');codeLines.push('func newPerson(name string) *person {\u000A');codeLines.push(' p :\u003D person{name: name}\u000A p.age \u003D 42\u000A return \u0026p\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fmt.Println(person{\"Bob\", 20})\u000A');codeLines.push(' fmt.Println(person{name: \"Alice\", age: 30})\u000A');codeLines.push(' fmt.Println(person{name: \"Fred\"})\u000A');codeLines.push(' fmt.Println(\u0026person{name: \"Ann\", age: 40})\u000A');codeLines.push(' fmt.Println(newPerson(\"Jon\"))\u000A');codeLines.push(' s :\u003D person{name: \"Sean\", age: 50}\u000A fmt.Println(s.name)\u000A');codeLines.push(' sp :\u003D \u0026s\u000A fmt.Println(sp.age)\u000A');codeLines.push(' sp.age \u003D 51\u000A fmt.Println(sp.age)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/switch generated
View File

@ -203,7 +203,7 @@ type corresponding to its clause.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' i :\x3D 2\u000A fmt.Print(\"Write \", i, \" as \")\u000A switch i {\u000A case 1:\u000A fmt.Println(\"one\")\u000A case 2:\u000A fmt.Println(\"two\")\u000A case 3:\u000A fmt.Println(\"three\")\u000A }\u000A');codeLines.push(' switch time.Now().Weekday() {\u000A case time.Saturday, time.Sunday:\u000A fmt.Println(\"It\'s the weekend\")\u000A default:\u000A fmt.Println(\"It\'s a weekday\")\u000A }\u000A');codeLines.push(' t :\x3D time.Now()\u000A switch {\u000A case t.Hour() \x3C 12:\u000A fmt.Println(\"It\'s before noon\")\u000A default:\u000A fmt.Println(\"It\'s after noon\")\u000A }\u000A');codeLines.push(' whatAmI :\x3D func(i interface{}) {\u000A switch t :\x3D i.(type) {\u000A case bool:\u000A fmt.Println(\"I\'m a bool\")\u000A case int:\u000A fmt.Println(\"I\'m an int\")\u000A default:\u000A fmt.Printf(\"Don\'t know type %T\\n\", t)\u000A }\u000A }\u000A whatAmI(true)\u000A whatAmI(1)\u000A whatAmI(\"hey\")\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' i :\u003D 2\u000A fmt.Print(\"Write \", i, \" as \")\u000A switch i {\u000A case 1:\u000A fmt.Println(\"one\")\u000A case 2:\u000A fmt.Println(\"two\")\u000A case 3:\u000A fmt.Println(\"three\")\u000A }\u000A');codeLines.push(' switch time.Now().Weekday() {\u000A case time.Saturday, time.Sunday:\u000A fmt.Println(\"It\'s the weekend\")\u000A default:\u000A fmt.Println(\"It\'s a weekday\")\u000A }\u000A');codeLines.push(' t :\u003D time.Now()\u000A switch {\u000A case t.Hour() \u003C 12:\u000A fmt.Println(\"It\'s before noon\")\u000A default:\u000A fmt.Println(\"It\'s after noon\")\u000A }\u000A');codeLines.push(' whatAmI :\u003D func(i interface{}) {\u000A switch t :\u003D i.(type) {\u000A case bool:\u000A fmt.Println(\"I\'m a bool\")\u000A case int:\u000A fmt.Println(\"I\'m an int\")\u000A default:\u000A fmt.Printf(\"Don\'t know type %T\\n\", t)\u000A }\u000A }\u000A whatAmI(true)\u000A whatAmI(1)\u000A whatAmI(\"hey\")\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -242,7 +242,7 @@ prefixing them with our temporary directory.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"io/ioutil\"\u000A \"os\"\u000A \"path/filepath\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\x3D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f, err :\x3D ioutil.TempFile(\"\", \"sample\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Temp file name:\", f.Name())\u000A');codeLines.push(' defer os.Remove(f.Name())\u000A');codeLines.push(' _, err \x3D f.Write([]byte{1, 2, 3, 4})\u000A check(err)\u000A');codeLines.push(' dname, err :\x3D ioutil.TempDir(\"\", \"sampledir\")\u000A check(err)\u000A fmt.Println(\"Temp dir name:\", dname)\u000A');codeLines.push(' defer os.RemoveAll(dname)\u000A');codeLines.push(' fname :\x3D filepath.Join(dname, \"file1\")\u000A err \x3D ioutil.WriteFile(fname, []byte{1, 2}, 0666)\u000A check(err)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"io/ioutil\"\u000A \"os\"\u000A \"path/filepath\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\u003D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f, err :\u003D ioutil.TempFile(\"\", \"sample\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Temp file name:\", f.Name())\u000A');codeLines.push(' defer os.Remove(f.Name())\u000A');codeLines.push(' _, err \u003D f.Write([]byte{1, 2, 3, 4})\u000A check(err)\u000A');codeLines.push(' dname, err :\u003D ioutil.TempDir(\"\", \"sampledir\")\u000A check(err)\u000A fmt.Println(\"Temp dir name:\", dname)\u000A');codeLines.push(' defer os.RemoveAll(dname)\u000A');codeLines.push(' fname :\u003D filepath.Join(dname, \"file1\")\u000A err \u003D ioutil.WriteFile(fname, []byte{1, 2}, 0666)\u000A check(err)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/testing generated
View File

@ -231,7 +231,7 @@ when executing <code>go test -v</code>.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"testing\"\u000A)\u000A');codeLines.push('func IntMin(a, b int) int {\u000A if a \x3C b {\u000A return a\u000A }\u000A return b\u000A}\u000A');codeLines.push('func TestIntMinBasic(t *testing.T) {\u000A ans :\x3D IntMin(2, -2)\u000A if ans !\x3D -2 {\u000A');codeLines.push(' t.Errorf(\"IntMin(2, -2) \x3D %d; want -2\", ans)\u000A }\u000A}\u000A');codeLines.push('func TestIntMinTableDriven(t *testing.T) {\u000A var tests \x3D []struct {\u000A a, b int\u000A want int\u000A }{\u000A {0, 1, 0},\u000A {1, 0, 0},\u000A {2, -2, -2},\u000A {0, -1, -1},\u000A {-1, 0, -1},\u000A }\u000A');codeLines.push(' for _, tt :\x3D range tests {\u000A');codeLines.push(' testname :\x3D fmt.Sprintf(\"%d,%d\", tt.a, tt.b)\u000A t.Run(testname, func(t *testing.T) {\u000A ans :\x3D IntMin(tt.a, tt.b)\u000A if ans !\x3D tt.want {\u000A t.Errorf(\"got %d, want %d\", ans, tt.want)\u000A }\u000A })\u000A }\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"testing\"\u000A)\u000A');codeLines.push('func IntMin(a, b int) int {\u000A if a \u003C b {\u000A return a\u000A }\u000A return b\u000A}\u000A');codeLines.push('func TestIntMinBasic(t *testing.T) {\u000A ans :\u003D IntMin(2, -2)\u000A if ans !\u003D -2 {\u000A');codeLines.push(' t.Errorf(\"IntMin(2, -2) \u003D %d; want -2\", ans)\u000A }\u000A}\u000A');codeLines.push('func TestIntMinTableDriven(t *testing.T) {\u000A var tests \u003D []struct {\u000A a, b int\u000A want int\u000A }{\u000A {0, 1, 0},\u000A {1, 0, 0},\u000A {2, -2, -2},\u000A {0, -1, -1},\u000A {-1, 0, -1},\u000A }\u000A');codeLines.push(' for _, tt :\u003D range tests {\u000A');codeLines.push(' testname :\u003D fmt.Sprintf(\"%d,%d\", tt.a, tt.b)\u000A t.Run(testname, func(t *testing.T) {\u000A ans :\u003D IntMin(tt.a, tt.b)\u000A if ans !\u003D tt.want {\u000A t.Errorf(\"got %d, want %d\", ans, tt.want)\u000A }\u000A })\u000A }\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/tickers generated
View File

@ -171,7 +171,7 @@ before we stop it.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' ticker :\x3D time.NewTicker(500 * time.Millisecond)\u000A done :\x3D make(chan bool)\u000A');codeLines.push(' go func() {\u000A for {\u000A select {\u000A case \x3C-done:\u000A return\u000A case t :\x3D \x3C-ticker.C:\u000A fmt.Println(\"Tick at\", t)\u000A }\u000A }\u000A }()\u000A');codeLines.push(' time.Sleep(1600 * time.Millisecond)\u000A ticker.Stop()\u000A done \x3C- true\u000A fmt.Println(\"Ticker stopped\")\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' ticker :\u003D time.NewTicker(500 * time.Millisecond)\u000A done :\u003D make(chan bool)\u000A');codeLines.push(' go func() {\u000A for {\u000A select {\u000A case \u003C-done:\u000A return\u000A case t :\u003D \u003C-ticker.C:\u000A fmt.Println(\"Tick at\", t)\u000A }\u000A }\u000A }()\u000A');codeLines.push(' time.Sleep(1600 * time.Millisecond)\u000A ticker.Stop()\u000A done \u003C- true\u000A fmt.Println(\"Ticker stopped\")\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/time generated
View File

@ -270,7 +270,7 @@ the Unix epoch.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A p :\x3D fmt.Println\u000A');codeLines.push(' now :\x3D time.Now()\u000A p(now)\u000A');codeLines.push(' then :\x3D time.Date(\u000A 2009, 11, 17, 20, 34, 58, 651387237, time.UTC)\u000A p(then)\u000A');codeLines.push(' p(then.Year())\u000A p(then.Month())\u000A p(then.Day())\u000A p(then.Hour())\u000A p(then.Minute())\u000A p(then.Second())\u000A p(then.Nanosecond())\u000A p(then.Location())\u000A');codeLines.push(' p(then.Weekday())\u000A');codeLines.push(' p(then.Before(now))\u000A p(then.After(now))\u000A p(then.Equal(now))\u000A');codeLines.push(' diff :\x3D now.Sub(then)\u000A p(diff)\u000A');codeLines.push(' p(diff.Hours())\u000A p(diff.Minutes())\u000A p(diff.Seconds())\u000A p(diff.Nanoseconds())\u000A');codeLines.push(' p(then.Add(diff))\u000A p(then.Add(-diff))\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A p :\u003D fmt.Println\u000A');codeLines.push(' now :\u003D time.Now()\u000A p(now)\u000A');codeLines.push(' then :\u003D time.Date(\u000A 2009, 11, 17, 20, 34, 58, 651387237, time.UTC)\u000A p(then)\u000A');codeLines.push(' p(then.Year())\u000A p(then.Month())\u000A p(then.Day())\u000A p(then.Hour())\u000A p(then.Minute())\u000A p(then.Second())\u000A p(then.Nanosecond())\u000A p(then.Location())\u000A');codeLines.push(' p(then.Weekday())\u000A');codeLines.push(' p(then.Before(now))\u000A p(then.After(now))\u000A p(then.Equal(now))\u000A');codeLines.push(' diff :\u003D now.Sub(then)\u000A p(diff)\u000A');codeLines.push(' p(diff.Hours())\u000A p(diff.Minutes())\u000A p(diff.Seconds())\u000A p(diff.Nanoseconds())\u000A');codeLines.push(' p(then.Add(diff))\u000A p(then.Add(-diff))\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -204,7 +204,7 @@ explaining the parsing problem.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A p :\x3D fmt.Println\u000A');codeLines.push(' t :\x3D time.Now()\u000A p(t.Format(time.RFC3339))\u000A');codeLines.push(' t1, e :\x3D time.Parse(\u000A time.RFC3339,\u000A \"2012-11-01T22:08:41+00:00\")\u000A p(t1)\u000A');codeLines.push(' p(t.Format(\"3:04PM\"))\u000A p(t.Format(\"Mon Jan _2 15:04:05 2006\"))\u000A p(t.Format(\"2006-01-02T15:04:05.999999-07:00\"))\u000A form :\x3D \"3 04 PM\"\u000A t2, e :\x3D time.Parse(form, \"8 41 PM\")\u000A p(t2)\u000A');codeLines.push(' fmt.Printf(\"%d-%02d-%02dT%02d:%02d:%02d-00:00\\n\",\u000A t.Year(), t.Month(), t.Day(),\u000A t.Hour(), t.Minute(), t.Second())\u000A');codeLines.push(' ansic :\x3D \"Mon Jan _2 15:04:05 2006\"\u000A _, e \x3D time.Parse(ansic, \"8:41PM\")\u000A p(e)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A p :\u003D fmt.Println\u000A');codeLines.push(' t :\u003D time.Now()\u000A p(t.Format(time.RFC3339))\u000A');codeLines.push(' t1, e :\u003D time.Parse(\u000A time.RFC3339,\u000A \"2012-11-01T22:08:41+00:00\")\u000A p(t1)\u000A');codeLines.push(' p(t.Format(\"3:04PM\"))\u000A p(t.Format(\"Mon Jan _2 15:04:05 2006\"))\u000A p(t.Format(\"2006-01-02T15:04:05.999999-07:00\"))\u000A form :\u003D \"3 04 PM\"\u000A t2, e :\u003D time.Parse(form, \"8 41 PM\")\u000A p(t2)\u000A');codeLines.push(' fmt.Printf(\"%d-%02d-%02dT%02d:%02d:%02d-00:00\\n\",\u000A t.Year(), t.Month(), t.Day(),\u000A t.Hour(), t.Minute(), t.Second())\u000A');codeLines.push(' ansic :\u003D \"Mon Jan _2 15:04:05 2006\"\u000A _, e \u003D time.Parse(ansic, \"8:41PM\")\u000A p(e)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/timeouts generated
View File

@ -181,7 +181,7 @@ out and the second succeeding.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' c1 :\x3D make(chan string, 1)\u000A go func() {\u000A time.Sleep(2 * time.Second)\u000A c1 \x3C- \"result 1\"\u000A }()\u000A');codeLines.push(' select {\u000A case res :\x3D \x3C-c1:\u000A fmt.Println(res)\u000A case \x3C-time.After(1 * time.Second):\u000A fmt.Println(\"timeout 1\")\u000A }\u000A');codeLines.push(' c2 :\x3D make(chan string, 1)\u000A go func() {\u000A time.Sleep(2 * time.Second)\u000A c2 \x3C- \"result 2\"\u000A }()\u000A select {\u000A case res :\x3D \x3C-c2:\u000A fmt.Println(res)\u000A case \x3C-time.After(3 * time.Second):\u000A fmt.Println(\"timeout 2\")\u000A }\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' c1 :\u003D make(chan string, 1)\u000A go func() {\u000A time.Sleep(2 * time.Second)\u000A c1 \u003C- \"result 1\"\u000A }()\u000A');codeLines.push(' select {\u000A case res :\u003D \u003C-c1:\u000A fmt.Println(res)\u000A case \u003C-time.After(1 * time.Second):\u000A fmt.Println(\"timeout 1\")\u000A }\u000A');codeLines.push(' c2 :\u003D make(chan string, 1)\u000A go func() {\u000A time.Sleep(2 * time.Second)\u000A c2 \u003C- \"result 2\"\u000A }()\u000A select {\u000A case res :\u003D \u003C-c2:\u000A fmt.Println(res)\u000A case \u003C-time.After(3 * time.Second):\u000A fmt.Println(\"timeout 2\")\u000A }\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/timers generated
View File

@ -184,7 +184,7 @@ a chance to fire.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' timer1 :\x3D time.NewTimer(2 * time.Second)\u000A');codeLines.push(' \x3C-timer1.C\u000A fmt.Println(\"Timer 1 fired\")\u000A');codeLines.push(' timer2 :\x3D time.NewTimer(time.Second)\u000A go func() {\u000A \x3C-timer2.C\u000A fmt.Println(\"Timer 2 fired\")\u000A }()\u000A stop2 :\x3D timer2.Stop()\u000A if stop2 {\u000A fmt.Println(\"Timer 2 stopped\")\u000A }\u000A');codeLines.push(' time.Sleep(2 * time.Second)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' timer1 :\u003D time.NewTimer(2 * time.Second)\u000A');codeLines.push(' \u003C-timer1.C\u000A fmt.Println(\"Timer 1 fired\")\u000A');codeLines.push(' timer2 :\u003D time.NewTimer(time.Second)\u000A go func() {\u000A \u003C-timer2.C\u000A fmt.Println(\"Timer 2 fired\")\u000A }()\u000A stop2 :\u003D timer2.Stop()\u000A if stop2 {\u000A fmt.Println(\"Timer 2 stopped\")\u000A }\u000A');codeLines.push(' time.Sleep(2 * time.Second)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/url-parsing generated
View File

@ -235,7 +235,7 @@ pieces that we extracted.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net\"\u000A \"net/url\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' s :\x3D \"postgres://user:pass@host.com:5432/path?k\x3Dv#f\"\u000A');codeLines.push(' u, err :\x3D url.Parse(s)\u000A if err !\x3D nil {\u000A panic(err)\u000A }\u000A');codeLines.push(' fmt.Println(u.Scheme)\u000A');codeLines.push(' fmt.Println(u.User)\u000A fmt.Println(u.User.Username())\u000A p, _ :\x3D u.User.Password()\u000A fmt.Println(p)\u000A');codeLines.push(' fmt.Println(u.Host)\u000A host, port, _ :\x3D net.SplitHostPort(u.Host)\u000A fmt.Println(host)\u000A fmt.Println(port)\u000A');codeLines.push(' fmt.Println(u.Path)\u000A fmt.Println(u.Fragment)\u000A');codeLines.push(' fmt.Println(u.RawQuery)\u000A m, _ :\x3D url.ParseQuery(u.RawQuery)\u000A fmt.Println(m)\u000A fmt.Println(m[\"k\"][0])\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net\"\u000A \"net/url\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' s :\u003D \"postgres://user:pass@host.com:5432/path?k\u003Dv#f\"\u000A');codeLines.push(' u, err :\u003D url.Parse(s)\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A');codeLines.push(' fmt.Println(u.Scheme)\u000A');codeLines.push(' fmt.Println(u.User)\u000A fmt.Println(u.User.Username())\u000A p, _ :\u003D u.User.Password()\u000A fmt.Println(p)\u000A');codeLines.push(' fmt.Println(u.Host)\u000A host, port, _ :\u003D net.SplitHostPort(u.Host)\u000A fmt.Println(host)\u000A fmt.Println(port)\u000A');codeLines.push(' fmt.Println(u.Path)\u000A fmt.Println(u.Fragment)\u000A');codeLines.push(' fmt.Println(u.RawQuery)\u000A m, _ :\u003D url.ParseQuery(u.RawQuery)\u000A fmt.Println(m)\u000A fmt.Println(m[\"k\"][0])\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/values generated
View File

@ -152,7 +152,7 @@ basic examples.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fmt.Println(\"go\" + \"lang\")\u000A');codeLines.push(' fmt.Println(\"1+1 \x3D\", 1+1)\u000A fmt.Println(\"7.0/3.0 \x3D\", 7.0/3.0)\u000A');codeLines.push(' fmt.Println(true \x26\x26 false)\u000A fmt.Println(true || false)\u000A fmt.Println(!true)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fmt.Println(\"go\" + \"lang\")\u000A');codeLines.push(' fmt.Println(\"1+1 \u003D\", 1+1)\u000A fmt.Println(\"7.0/3.0 \u003D\", 7.0/3.0)\u000A');codeLines.push(' fmt.Println(true \u0026\u0026 false)\u000A fmt.Println(true || false)\u000A fmt.Println(!true)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/variables generated
View File

@ -183,7 +183,7 @@ initializing a variable, e.g. for
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var a \x3D \"initial\"\u000A fmt.Println(a)\u000A');codeLines.push(' var b, c int \x3D 1, 2\u000A fmt.Println(b, c)\u000A');codeLines.push(' var d \x3D true\u000A fmt.Println(d)\u000A');codeLines.push(' var e int\u000A fmt.Println(e)\u000A');codeLines.push(' f :\x3D \"apple\"\u000A fmt.Println(f)\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var a \u003D \"initial\"\u000A fmt.Println(a)\u000A');codeLines.push(' var b, c int \u003D 1, 2\u000A fmt.Println(b, c)\u000A');codeLines.push(' var d \u003D true\u000A fmt.Println(d)\u000A');codeLines.push(' var e int\u000A fmt.Println(e)\u000A');codeLines.push(' f :\u003D \"apple\"\u000A fmt.Println(f)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

View File

@ -172,7 +172,7 @@ to form closures, which we&rsquo;ll look at next.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func sum(nums ...int) {\u000A fmt.Print(nums, \" \")\u000A total :\x3D 0\u000A for _, num :\x3D range nums {\u000A total +\x3D num\u000A }\u000A fmt.Println(total)\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' sum(1, 2)\u000A sum(1, 2, 3)\u000A');codeLines.push(' nums :\x3D []int{1, 2, 3, 4}\u000A sum(nums...)\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func sum(nums ...int) {\u000A fmt.Print(nums, \" \")\u000A total :\u003D 0\u000A for _, num :\u003D range nums {\u000A total +\u003D num\u000A }\u000A fmt.Println(total)\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' sum(1, 2)\u000A sum(1, 2, 3)\u000A');codeLines.push(' nums :\u003D []int{1, 2, 3, 4}\u000A sum(nums...)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/waitgroups generated
View File

@ -229,7 +229,7 @@ is likely to be different for each invocation.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sync\"\u000A \"time\"\u000A)\u000A');codeLines.push('func worker(id int, wg *sync.WaitGroup) {\u000A');codeLines.push(' defer wg.Done()\u000A');codeLines.push(' fmt.Printf(\"Worker %d starting\\n\", id)\u000A');codeLines.push(' time.Sleep(time.Second)\u000A fmt.Printf(\"Worker %d done\\n\", id)\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i :\x3D 1; i \x3C\x3D 5; i++ {\u000A wg.Add(1)\u000A go worker(i, \x26wg)\u000A }\u000A');codeLines.push(' wg.Wait()\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sync\"\u000A \"time\"\u000A)\u000A');codeLines.push('func worker(id int, wg *sync.WaitGroup) {\u000A');codeLines.push(' defer wg.Done()\u000A');codeLines.push(' fmt.Printf(\"Worker %d starting\\n\", id)\u000A');codeLines.push(' time.Sleep(time.Second)\u000A fmt.Printf(\"Worker %d done\\n\", id)\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i :\u003D 1; i \u003C\u003D 5; i++ {\u000A wg.Add(1)\u000A go worker(i, \u0026wg)\u000A }\u000A');codeLines.push(' wg.Wait()\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/worker-pools generated
View File

@ -224,7 +224,7 @@ there are 3 workers operating concurrently.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func worker(id int, jobs \x3C-chan int, results chan\x3C- int) {\u000A for j :\x3D range jobs {\u000A fmt.Println(\"worker\", id, \"started job\", j)\u000A time.Sleep(time.Second)\u000A fmt.Println(\"worker\", id, \"finished job\", j)\u000A results \x3C- j * 2\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' const numJobs \x3D 5\u000A jobs :\x3D make(chan int, numJobs)\u000A results :\x3D make(chan int, numJobs)\u000A');codeLines.push(' for w :\x3D 1; w \x3C\x3D 3; w++ {\u000A go worker(w, jobs, results)\u000A }\u000A');codeLines.push(' for j :\x3D 1; j \x3C\x3D numJobs; j++ {\u000A jobs \x3C- j\u000A }\u000A close(jobs)\u000A');codeLines.push(' for a :\x3D 1; a \x3C\x3D numJobs; a++ {\u000A \x3C-results\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func worker(id int, jobs \u003C-chan int, results chan\u003C- int) {\u000A for j :\u003D range jobs {\u000A fmt.Println(\"worker\", id, \"started job\", j)\u000A time.Sleep(time.Second)\u000A fmt.Println(\"worker\", id, \"finished job\", j)\u000A results \u003C- j * 2\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' const numJobs \u003D 5\u000A jobs :\u003D make(chan int, numJobs)\u000A results :\u003D make(chan int, numJobs)\u000A');codeLines.push(' for w :\u003D 1; w \u003C\u003D 3; w++ {\u000A go worker(w, jobs, results)\u000A }\u000A');codeLines.push(' for j :\u003D 1; j \u003C\u003D numJobs; j++ {\u000A jobs \u003C- j\u000A }\u000A close(jobs)\u000A');codeLines.push(' for a :\u003D 1; a \u003C\u003D numJobs; a++ {\u000A \u003C-results\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/writing-files generated
View File

@ -289,7 +289,7 @@ we&rsquo;ve just seen to the <code>stdin</code> and <code>stdout</code> streams.
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"io/ioutil\"\u000A \"os\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\x3D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' d1 :\x3D []byte(\"hello\\ngo\\n\")\u000A err :\x3D ioutil.WriteFile(\"/tmp/dat1\", d1, 0644)\u000A check(err)\u000A');codeLines.push(' f, err :\x3D os.Create(\"/tmp/dat2\")\u000A check(err)\u000A');codeLines.push(' defer f.Close()\u000A');codeLines.push(' d2 :\x3D []byte{115, 111, 109, 101, 10}\u000A n2, err :\x3D f.Write(d2)\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n2)\u000A');codeLines.push(' n3, err :\x3D f.WriteString(\"writes\\n\")\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n3)\u000A');codeLines.push(' f.Sync()\u000A');codeLines.push(' w :\x3D bufio.NewWriter(f)\u000A n4, err :\x3D w.WriteString(\"buffered\\n\")\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n4)\u000A');codeLines.push(' w.Flush()\u000A');codeLines.push('}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"io/ioutil\"\u000A \"os\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\u003D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' d1 :\u003D []byte(\"hello\\ngo\\n\")\u000A err :\u003D ioutil.WriteFile(\"/tmp/dat1\", d1, 0644)\u000A check(err)\u000A');codeLines.push(' f, err :\u003D os.Create(\"/tmp/dat2\")\u000A check(err)\u000A');codeLines.push(' defer f.Close()\u000A');codeLines.push(' d2 :\u003D []byte{115, 111, 109, 101, 10}\u000A n2, err :\u003D f.Write(d2)\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n2)\u000A');codeLines.push(' n3, err :\u003D f.WriteString(\"writes\\n\")\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n3)\u000A');codeLines.push(' f.Sync()\u000A');codeLines.push(' w :\u003D bufio.NewWriter(f)\u000A n4, err :\u003D w.WriteString(\"buffered\\n\")\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n4)\u000A');codeLines.push(' w.Flush()\u000A');codeLines.push('}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>

2
public/xml generated
View File

@ -281,7 +281,7 @@ to nest all <code>plant</code>s under <code>&lt;parent&gt;&lt;child&gt;...</code
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"encoding/xml\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('type Plant struct {\u000A XMLName xml.Name `xml:\"plant\"`\u000A Id int `xml:\"id,attr\"`\u000A Name string `xml:\"name\"`\u000A Origin []string `xml:\"origin\"`\u000A}\u000A');codeLines.push('func (p Plant) String() string {\u000A return fmt.Sprintf(\"Plant id\x3D%v, name\x3D%v, origin\x3D%v\",\u000A p.Id, p.Name, p.Origin)\u000A}\u000A');codeLines.push('func main() {\u000A coffee :\x3D \x26Plant{Id: 27, Name: \"Coffee\"}\u000A coffee.Origin \x3D []string{\"Ethiopia\", \"Brazil\"}\u000A');codeLines.push(' out, _ :\x3D xml.MarshalIndent(coffee, \" \", \" \")\u000A fmt.Println(string(out))\u000A');codeLines.push(' fmt.Println(xml.Header + string(out))\u000A');codeLines.push(' var p Plant\u000A if err :\x3D xml.Unmarshal(out, \x26p); err !\x3D nil {\u000A panic(err)\u000A }\u000A fmt.Println(p)\u000A');codeLines.push(' tomato :\x3D \x26Plant{Id: 81, Name: \"Tomato\"}\u000A tomato.Origin \x3D []string{\"Mexico\", \"California\"}\u000A');codeLines.push(' type Nesting struct {\u000A XMLName xml.Name `xml:\"nesting\"`\u000A Plants []*Plant `xml:\"parent\x3Echild\x3Eplant\"`\u000A }\u000A');codeLines.push(' nesting :\x3D \x26Nesting{}\u000A nesting.Plants \x3D []*Plant{coffee, tomato}\u000A');codeLines.push(' out, _ \x3D xml.MarshalIndent(nesting, \" \", \" \")\u000A fmt.Println(string(out))\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"encoding/xml\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('type Plant struct {\u000A XMLName xml.Name `xml:\"plant\"`\u000A Id int `xml:\"id,attr\"`\u000A Name string `xml:\"name\"`\u000A Origin []string `xml:\"origin\"`\u000A}\u000A');codeLines.push('func (p Plant) String() string {\u000A return fmt.Sprintf(\"Plant id\u003D%v, name\u003D%v, origin\u003D%v\",\u000A p.Id, p.Name, p.Origin)\u000A}\u000A');codeLines.push('func main() {\u000A coffee :\u003D \u0026Plant{Id: 27, Name: \"Coffee\"}\u000A coffee.Origin \u003D []string{\"Ethiopia\", \"Brazil\"}\u000A');codeLines.push(' out, _ :\u003D xml.MarshalIndent(coffee, \" \", \" \")\u000A fmt.Println(string(out))\u000A');codeLines.push(' fmt.Println(xml.Header + string(out))\u000A');codeLines.push(' var p Plant\u000A if err :\u003D xml.Unmarshal(out, \u0026p); err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(p)\u000A');codeLines.push(' tomato :\u003D \u0026Plant{Id: 81, Name: \"Tomato\"}\u000A tomato.Origin \u003D []string{\"Mexico\", \"California\"}\u000A');codeLines.push(' type Nesting struct {\u000A XMLName xml.Name `xml:\"nesting\"`\u000A Plants []*Plant `xml:\"parent\u003Echild\u003Eplant\"`\u000A }\u000A');codeLines.push(' nesting :\u003D \u0026Nesting{}\u000A nesting.Plants \u003D []*Plant{coffee, tomato}\u000A');codeLines.push(' out, _ \u003D xml.MarshalIndent(nesting, \" \", \" \")\u000A fmt.Println(string(out))\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>