Regenerate
This commit is contained in:
parent
3f8e58ad58
commit
a1fd3bf037
2
public/arrays
generated
2
public/arrays
generated
@ -206,7 +206,7 @@ typical Go. We’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] = 100\u000A fmt.Println(\"set:\", a)\u000A fmt.Println(\"get:\", a[4])\u000A');codeLines.push(' fmt.Println(\"len:\", len(a))\u000A');codeLines.push(' b := [5]int{1, 2, 3, 4, 5}\u000A fmt.Println(\"dcl:\", b)\u000A');codeLines.push(' var twoD [2][3]int\u000A for i := 0; i \x3C 2; i++ {\u000A for j := 0; j \x3C 3; j++ {\u000A twoD[i][j] = 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] \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/atomic-counters
generated
2
public/atomic-counters
generated
@ -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 := 0; i \x3C 50; i++ {\u000A wg.Add(1)\u000A');codeLines.push(' go func() {\u000A for c := 0; c \x3C 1000; c++ {\u000A');codeLines.push(' atomic.AddUint64(&ops, 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/base64-encoding
generated
2
public/base64-encoding
generated
@ -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 := \"abc123!?$*&()\'-=@~\"\u000A');codeLines.push(' sEnc := b64.StdEncoding.EncodeToString([]byte(data))\u000A fmt.Println(sEnc)\u000A');codeLines.push(' sDec, _ := b64.StdEncoding.DecodeString(sEnc)\u000A fmt.Println(string(sDec))\u000A fmt.Println()\u000A');codeLines.push(' uEnc := b64.URLEncoding.EncodeToString([]byte(data))\u000A fmt.Println(uEnc)\u000A uDec, _ := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/channel-buffering
generated
2
public/channel-buffering
generated
@ -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 := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/channel-directions
generated
2
public/channel-directions
generated
@ -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 := \x3C-pings\u000A pongs \x3C- msg\u000A}\u000A');codeLines.push('func main() {\u000A pings := make(chan string, 1)\u000A pongs := 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\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/channel-synchronization
generated
2
public/channel-synchronization
generated
@ -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 := 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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/channels
generated
2
public/channels
generated
@ -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 := make(chan string)\u000A');codeLines.push(' go func() { messages \x3C- \"ping\" }()\u000A');codeLines.push(' msg := \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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/closing-channels
generated
2
public/closing-channels
generated
@ -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 := make(chan int, 5)\u000A done := make(chan bool)\u000A');codeLines.push(' go func() {\u000A for {\u000A j, more := \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 := 1; j \x3C= 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/closures
generated
2
public/closures
generated
@ -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 := 0\u000A return func() int {\u000A i++\u000A return i\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' nextInt := intSeq()\u000A');codeLines.push(' fmt.Println(nextInt())\u000A fmt.Println(nextInt())\u000A fmt.Println(nextInt())\u000A');codeLines.push(' newInts := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/collection-functions
generated
2
public/collection-functions
generated
@ -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 := range vs {\u000A if v == 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= 0\u000A}\u000A');codeLines.push('func Any(vs []string, f func(string) bool) bool {\u000A for _, v := 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 := 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 := make([]string, 0)\u000A for _, v := range vs {\u000A if f(v) {\u000A vsf = append(vsf, v)\u000A }\u000A }\u000A return vsf\u000A}\u000A');codeLines.push('func Map(vs []string, f func(string) string) []string {\u000A vsm := make([]string, len(vs))\u000A for i, v := range vs {\u000A vsm[i] = f(v)\u000A }\u000A return vsm\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var strs = []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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/command-line-arguments
generated
2
public/command-line-arguments
generated
@ -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 := os.Args\u000A argsWithoutProg := os.Args[1:]\u000A');codeLines.push(' arg := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/command-line-flags
generated
2
public/command-line-flags
generated
@ -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 := flag.String(\"word\", \"foo\", \"a string\")\u000A');codeLines.push(' numbPtr := flag.Int(\"numb\", 42, \"an int\")\u000A boolPtr := flag.Bool(\"fork\", false, \"a bool\")\u000A');codeLines.push(' var svar string\u000A flag.StringVar(&svar, \"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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/command-line-subcommands
generated
2
public/command-line-subcommands
generated
@ -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 := flag.NewFlagSet(\"foo\", flag.ExitOnError)\u000A fooEnable := fooCmd.Bool(\"enable\", false, \"enable\")\u000A fooName := fooCmd.String(\"name\", \"\", \"name\")\u000A');codeLines.push(' barCmd := flag.NewFlagSet(\"bar\", flag.ExitOnError)\u000A barLevel := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/constants
generated
2
public/constants
generated
@ -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 = \"constant\"\u000A');codeLines.push('func main() {\u000A fmt.Println(s)\u000A');codeLines.push(' const n = 500000000\u000A');codeLines.push(' const d = 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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/context
generated
2
public/context
generated
@ -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 := 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 := ctx.Err()\u000A fmt.Println(\"server:\", err)\u000A internalError := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/defer
generated
2
public/defer
generated
@ -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 := 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 := os.Create(p)\u000A if err != 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 := f.Close()\u000A');codeLines.push(' if err != 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/directories
generated
2
public/directories
generated
@ -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 != nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' err := os.Mkdir(\"subdir\", 0755)\u000A check(err)\u000A');codeLines.push(' defer os.RemoveAll(\"subdir\")\u000A');codeLines.push(' createEmptyFile := func(name string) {\u000A d := []byte(\"\")\u000A check(ioutil.WriteFile(name, d, 0644))\u000A }\u000A');codeLines.push(' createEmptyFile(\"subdir/file1\")\u000A');codeLines.push(' err = 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 := ioutil.ReadDir(\"subdir/parent\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Listing subdir/parent\")\u000A for _, entry := range c {\u000A fmt.Println(\" \", entry.Name(), entry.IsDir())\u000A }\u000A');codeLines.push(' err = os.Chdir(\"subdir/parent/child\")\u000A check(err)\u000A');codeLines.push(' c, err = ioutil.ReadDir(\".\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Listing subdir/parent/child\")\u000A for _, entry := range c {\u000A fmt.Println(\" \", entry.Name(), entry.IsDir())\u000A }\u000A');codeLines.push(' err = os.Chdir(\"../../..\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Visiting subdir\")\u000A err = filepath.Walk(\"subdir\", visit)\u000A}\u000A');codeLines.push('func visit(p string, info os.FileInfo, err error) error {\u000A if err != 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 !\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/environment-variables
generated
2
public/environment-variables
generated
@ -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 := range os.Environ() {\u000A pair := strings.SplitN(e, \"=\", 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/epoch
generated
2
public/epoch
generated
@ -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 := time.Now()\u000A secs := now.Unix()\u000A nanos := now.UnixNano()\u000A fmt.Println(now)\u000A');codeLines.push(' millis := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/errors
generated
2
public/errors
generated
@ -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 == 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 == 42 {\u000A');codeLines.push(' return -1, &argError{arg, \"can\'t work with it\"}\u000A }\u000A return arg + 3, nil\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' for _, i := range []int{7, 42} {\u000A if r, e := f1(i); e != nil {\u000A fmt.Println(\"f1 failed:\", e)\u000A } else {\u000A fmt.Println(\"f1 worked:\", r)\u000A }\u000A }\u000A for _, i := range []int{7, 42} {\u000A if r, e := f2(i); e != nil {\u000A fmt.Println(\"f2 failed:\", e)\u000A } else {\u000A fmt.Println(\"f2 worked:\", r)\u000A }\u000A }\u000A');codeLines.push(' _, e := f2(42)\u000A if ae, ok := 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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/execing-processes
generated
2
public/execing-processes
generated
@ -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 := exec.LookPath(\"ls\")\u000A if lookErr != nil {\u000A panic(lookErr)\u000A }\u000A');codeLines.push(' args := []string{\"ls\", \"-a\", \"-l\", \"-h\"}\u000A');codeLines.push(' env := os.Environ()\u000A');codeLines.push(' execErr := syscall.Exec(binary, args, env)\u000A if execErr != 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/file-paths
generated
2
public/file-paths
generated
@ -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 := 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 := \"config.json\"\u000A');codeLines.push(' ext := filepath.Ext(filename)\u000A fmt.Println(ext)\u000A');codeLines.push(' fmt.Println(strings.TrimSuffix(filename, ext))\u000A');codeLines.push(' rel, err := filepath.Rel(\"a/b\", \"a/b/t/file\")\u000A if err != nil {\u000A panic(err)\u000A }\u000A fmt.Println(rel)\u000A');codeLines.push(' rel, err = filepath.Rel(\"a/b\", \"a/c/t/file\")\u000A if err != 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/for
generated
2
public/for
generated
@ -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 := 1\u000A for i \x3C= 3 {\u000A fmt.Println(i)\u000A i = i + 1\u000A }\u000A');codeLines.push(' for j := 7; j \x3C= 9; j++ {\u000A fmt.Println(j)\u000A }\u000A');codeLines.push(' for {\u000A fmt.Println(\"loop\")\u000A break\u000A }\u000A');codeLines.push(' for n := 0; n \x3C= 5; n++ {\u000A if n%2 == 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/functions
generated
2
public/functions
generated
@ -193,7 +193,7 @@ multiple return values, which we’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 := plus(1, 2)\u000A fmt.Println(\"1+2 =\", res)\u000A');codeLines.push(' res = plusPlus(1, 2, 3)\u000A fmt.Println(\"1+2+3 =\", 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/goroutines
generated
2
public/goroutines
generated
@ -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 := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/http-clients
generated
2
public/http-clients
generated
@ -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 := http.Get(\"http://gobyexample.com\")\u000A if err != nil {\u000A panic(err)\u000A }\u000A defer resp.Body.Close()\u000A');codeLines.push(' fmt.Println(\"Response status:\", resp.Status)\u000A');codeLines.push(' scanner := bufio.NewScanner(resp.Body)\u000A for i := 0; scanner.Scan() && i \x3C 5; i++ {\u000A fmt.Println(scanner.Text())\u000A }\u000A');codeLines.push(' if err := scanner.Err(); err != 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/http-servers
generated
2
public/http-servers
generated
@ -210,7 +210,7 @@ router we’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 := range req.Header {\u000A for _, h := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/if-else
generated
2
public/if-else
generated
@ -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 == 0 {\u000A fmt.Println(\"7 is even\")\u000A } else {\u000A fmt.Println(\"7 is odd\")\u000A }\u000A');codeLines.push(' if 8%4 == 0 {\u000A fmt.Println(\"8 is divisible by 4\")\u000A }\u000A');codeLines.push(' if num := 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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/interfaces
generated
2
public/interfaces
generated
@ -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 := rect{width: 3, height: 4}\u000A c := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/json
generated
2
public/json
generated
@ -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, _ := json.Marshal(true)\u000A fmt.Println(string(bolB))\u000A');codeLines.push(' intB, _ := json.Marshal(1)\u000A fmt.Println(string(intB))\u000A');codeLines.push(' fltB, _ := json.Marshal(2.34)\u000A fmt.Println(string(fltB))\u000A');codeLines.push(' strB, _ := json.Marshal(\"gopher\")\u000A fmt.Println(string(strB))\u000A');codeLines.push(' slcD := []string{\"apple\", \"peach\", \"pear\"}\u000A slcB, _ := json.Marshal(slcD)\u000A fmt.Println(string(slcB))\u000A');codeLines.push(' mapD := map[string]int{\"apple\": 5, \"lettuce\": 7}\u000A mapB, _ := json.Marshal(mapD)\u000A fmt.Println(string(mapB))\u000A');codeLines.push(' res1D := &response1{\u000A Page: 1,\u000A Fruits: []string{\"apple\", \"peach\", \"pear\"}}\u000A res1B, _ := json.Marshal(res1D)\u000A fmt.Println(string(res1B))\u000A');codeLines.push(' res2D := &response2{\u000A Page: 1,\u000A Fruits: []string{\"apple\", \"peach\", \"pear\"}}\u000A res2B, _ := json.Marshal(res2D)\u000A fmt.Println(string(res2B))\u000A');codeLines.push(' byt := []byte(`{\"num\":6.13,\"strs\":[\"a\",\"b\"]}`)\u000A');codeLines.push(' var dat map[string]interface{}\u000A');codeLines.push(' if err := json.Unmarshal(byt, &dat); err != nil {\u000A panic(err)\u000A }\u000A fmt.Println(dat)\u000A');codeLines.push(' num := dat[\"num\"].(float64)\u000A fmt.Println(num)\u000A');codeLines.push(' strs := dat[\"strs\"].([]interface{})\u000A str1 := strs[0].(string)\u000A fmt.Println(str1)\u000A');codeLines.push(' str := `{\"page\": 1, \"fruits\": [\"apple\", \"peach\"]}`\u000A res := response2{}\u000A json.Unmarshal([]byte(str), &res)\u000A fmt.Println(res)\u000A fmt.Println(res.Fruits[0])\u000A');codeLines.push(' enc := json.NewEncoder(os.Stdout)\u000A d := 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, _ :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/line-filters
generated
2
public/line-filters
generated
@ -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 := bufio.NewScanner(os.Stdin)\u000A');codeLines.push(' for scanner.Scan() {\u000A');codeLines.push(' ucl := strings.ToUpper(scanner.Text())\u000A');codeLines.push(' fmt.Println(ucl)\u000A }\u000A');codeLines.push(' if err := scanner.Err(); err != 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/maps
generated
2
public/maps
generated
@ -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 := make(map[string]int)\u000A');codeLines.push(' m[\"k1\"] = 7\u000A m[\"k2\"] = 13\u000A');codeLines.push(' fmt.Println(\"map:\", m)\u000A');codeLines.push(' v1 := 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 := m[\"k2\"]\u000A fmt.Println(\"prs:\", prs)\u000A');codeLines.push(' n := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/methods
generated
2
public/methods
generated
@ -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 := rect{width: 10, height: 5}\u000A');codeLines.push(' fmt.Println(\"area: \", r.area())\u000A fmt.Println(\"perim:\", r.perim())\u000A');codeLines.push(' rp := &r\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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/multiple-return-values
generated
2
public/multiple-return-values
generated
@ -166,7 +166,7 @@ feature of Go functions; we’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 := vals()\u000A fmt.Println(a)\u000A fmt.Println(b)\u000A');codeLines.push(' _, c := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/mutexes
generated
2
public/mutexes
generated
@ -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 = make(map[int]int)\u000A');codeLines.push(' var mutex = &sync.Mutex{}\u000A');codeLines.push(' var readOps uint64\u000A var writeOps uint64\u000A');codeLines.push(' for r := 0; r \x3C 100; r++ {\u000A go func() {\u000A total := 0\u000A for {\u000A');codeLines.push(' key := rand.Intn(5)\u000A mutex.Lock()\u000A total += state[key]\u000A mutex.Unlock()\u000A atomic.AddUint64(&readOps, 1)\u000A');codeLines.push(' time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' for w := 0; w \x3C 10; w++ {\u000A go func() {\u000A for {\u000A key := rand.Intn(5)\u000A val := rand.Intn(100)\u000A mutex.Lock()\u000A state[key] = val\u000A mutex.Unlock()\u000A atomic.AddUint64(&writeOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' time.Sleep(time.Second)\u000A');codeLines.push(' readOpsFinal := atomic.LoadUint64(&readOps)\u000A fmt.Println(\"readOps:\", readOpsFinal)\u000A writeOpsFinal := atomic.LoadUint64(&writeOps)\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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/non-blocking-channel-operations
generated
2
public/non-blocking-channel-operations
generated
@ -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 := make(chan string)\u000A signals := make(chan bool)\u000A');codeLines.push(' select {\u000A case msg := \x3C-messages:\u000A fmt.Println(\"received message\", msg)\u000A default:\u000A fmt.Println(\"no message received\")\u000A }\u000A');codeLines.push(' msg := \"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 := \x3C-messages:\u000A fmt.Println(\"received message\", msg)\u000A case sig := \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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/number-parsing
generated
2
public/number-parsing
generated
@ -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, _ := strconv.ParseFloat(\"1.234\", 64)\u000A fmt.Println(f)\u000A');codeLines.push(' i, _ := strconv.ParseInt(\"123\", 0, 64)\u000A fmt.Println(i)\u000A');codeLines.push(' d, _ := strconv.ParseInt(\"0x1c8\", 0, 64)\u000A fmt.Println(d)\u000A');codeLines.push(' u, _ := strconv.ParseUint(\"789\", 0, 64)\u000A fmt.Println(u)\u000A');codeLines.push(' k, _ := strconv.Atoi(\"135\")\u000A fmt.Println(k)\u000A');codeLines.push(' _, e := 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, _ :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/panic
generated
2
public/panic
generated
@ -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 := os.Create(\"/tmp/file\")\u000A if err != 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 :\x3D os.Create(\"/tmp/file\")\u000A if err !\x3D 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
2
public/pointers
generated
@ -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 = 0\u000A}\u000A');codeLines.push('func zeroptr(iptr *int) {\u000A *iptr = 0\u000A}\u000A');codeLines.push('func main() {\u000A i := 1\u000A fmt.Println(\"initial:\", i)\u000A');codeLines.push(' zeroval(i)\u000A fmt.Println(\"zeroval:\", i)\u000A');codeLines.push(' zeroptr(&i)\u000A fmt.Println(\"zeroptr:\", i)\u000A');codeLines.push(' fmt.Println(\"pointer:\", &i)\u000A}\u000A');codeLines.push('');
|
||||
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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/random-numbers
generated
2
public/random-numbers
generated
@ -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 := rand.NewSource(time.Now().UnixNano())\u000A r1 := rand.New(s1)\u000A');codeLines.push(' fmt.Print(r1.Intn(100), \",\")\u000A fmt.Print(r1.Intn(100))\u000A fmt.Println()\u000A');codeLines.push(' s2 := rand.NewSource(42)\u000A r2 := rand.New(s2)\u000A fmt.Print(r2.Intn(100), \",\")\u000A fmt.Print(r2.Intn(100))\u000A fmt.Println()\u000A s3 := rand.NewSource(42)\u000A r3 := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/range
generated
2
public/range
generated
@ -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 := []int{2, 3, 4}\u000A sum := 0\u000A for _, num := range nums {\u000A sum += num\u000A }\u000A fmt.Println(\"sum:\", sum)\u000A');codeLines.push(' for i, num := range nums {\u000A if num == 3 {\u000A fmt.Println(\"index:\", i)\u000A }\u000A }\u000A');codeLines.push(' kvs := map[string]string{\"a\": \"apple\", \"b\": \"banana\"}\u000A for k, v := range kvs {\u000A fmt.Printf(\"%s -\x3E %s\\n\", k, v)\u000A }\u000A');codeLines.push(' for k := range kvs {\u000A fmt.Println(\"key:\", k)\u000A }\u000A');codeLines.push(' for i, c := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/range-over-channels
generated
2
public/range-over-channels
generated
@ -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 := make(chan string, 2)\u000A queue \x3C- \"one\"\u000A queue \x3C- \"two\"\u000A close(queue)\u000A');codeLines.push(' for elem := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/rate-limiting
generated
2
public/rate-limiting
generated
@ -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 := make(chan int, 5)\u000A for i := 1; i \x3C= 5; i++ {\u000A requests \x3C- i\u000A }\u000A close(requests)\u000A');codeLines.push(' limiter := time.Tick(200 * time.Millisecond)\u000A');codeLines.push(' for req := range requests {\u000A \x3C-limiter\u000A fmt.Println(\"request\", req, time.Now())\u000A }\u000A');codeLines.push(' burstyLimiter := make(chan time.Time, 3)\u000A');codeLines.push(' for i := 0; i \x3C 3; i++ {\u000A burstyLimiter \x3C- time.Now()\u000A }\u000A');codeLines.push(' go func() {\u000A for t := range time.Tick(200 * time.Millisecond) {\u000A burstyLimiter \x3C- t\u000A }\u000A }()\u000A');codeLines.push(' burstyRequests := make(chan int, 5)\u000A for i := 1; i \x3C= 5; i++ {\u000A burstyRequests \x3C- i\u000A }\u000A close(burstyRequests)\u000A for req := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/reading-files
generated
2
public/reading-files
generated
@ -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 != nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' dat, err := ioutil.ReadFile(\"/tmp/dat\")\u000A check(err)\u000A fmt.Print(string(dat))\u000A');codeLines.push(' f, err := os.Open(\"/tmp/dat\")\u000A check(err)\u000A');codeLines.push(' b1 := make([]byte, 5)\u000A n1, err := f.Read(b1)\u000A check(err)\u000A fmt.Printf(\"%d bytes: %s\\n\", n1, string(b1[:n1]))\u000A');codeLines.push(' o2, err := f.Seek(6, 0)\u000A check(err)\u000A b2 := make([]byte, 2)\u000A n2, err := 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 := f.Seek(6, 0)\u000A check(err)\u000A b3 := make([]byte, 2)\u000A n3, err := io.ReadAtLeast(f, b3, 2)\u000A check(err)\u000A fmt.Printf(\"%d bytes @ %d: %s\\n\", n3, o3, string(b3))\u000A');codeLines.push(' _, err = f.Seek(0, 0)\u000A check(err)\u000A');codeLines.push(' r4 := bufio.NewReader(f)\u000A b4, err := 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 !\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/recursion
generated
2
public/recursion
generated
@ -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 == 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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/regular-expressions
generated
2
public/regular-expressions
generated
@ -342,7 +342,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, _ := regexp.MatchString(\"p([a-z]+)ch\", \"peach\")\u000A fmt.Println(match)\u000A');codeLines.push(' r, _ := 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 = 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 := []byte(\"a peach\")\u000A out := 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, _ :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/select
generated
2
public/select
generated
@ -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 := make(chan string)\u000A c2 := 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 := 0; i \x3C 2; i++ {\u000A select {\u000A case msg1 := \x3C-c1:\u000A fmt.Println(\"received\", msg1)\u000A case msg2 := \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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/sha1-hashes
generated
2
public/sha1-hashes
generated
@ -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 := \"sha1 this string\"\u000A');codeLines.push(' h := sha1.New()\u000A');codeLines.push(' h.Write([]byte(s))\u000A');codeLines.push(' bs := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/signals
generated
2
public/signals
generated
@ -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 := make(chan os.Signal, 1)\u000A done := make(chan bool, 1)\u000A');codeLines.push(' signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)\u000A');codeLines.push(' go func() {\u000A sig := \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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/slices
generated
2
public/slices
generated
@ -308,7 +308,7 @@ Go’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 := make([]string, 3)\u000A fmt.Println(\"emp:\", s)\u000A');codeLines.push(' s[0] = \"a\"\u000A s[1] = \"b\"\u000A s[2] = \"c\"\u000A fmt.Println(\"set:\", s)\u000A fmt.Println(\"get:\", s[2])\u000A');codeLines.push(' fmt.Println(\"len:\", len(s))\u000A');codeLines.push(' s = append(s, \"d\")\u000A s = append(s, \"e\", \"f\")\u000A fmt.Println(\"apd:\", s)\u000A');codeLines.push(' c := make([]string, len(s))\u000A copy(c, s)\u000A fmt.Println(\"cpy:\", c)\u000A');codeLines.push(' l := s[2:5]\u000A fmt.Println(\"sl1:\", l)\u000A');codeLines.push(' l = s[:5]\u000A fmt.Println(\"sl2:\", l)\u000A');codeLines.push(' l = s[2:]\u000A fmt.Println(\"sl3:\", l)\u000A');codeLines.push(' t := []string{\"g\", \"h\", \"i\"}\u000A fmt.Println(\"dcl:\", t)\u000A');codeLines.push(' twoD := make([][]int, 3)\u000A for i := 0; i \x3C 3; i++ {\u000A innerLen := i + 1\u000A twoD[i] = make([]int, innerLen)\u000A for j := 0; j \x3C innerLen; j++ {\u000A twoD[i][j] = 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/sorting
generated
2
public/sorting
generated
@ -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 := []string{\"c\", \"a\", \"b\"}\u000A sort.Strings(strs)\u000A fmt.Println(\"Strings:\", strs)\u000A');codeLines.push(' ints := []int{7, 2, 4}\u000A sort.Ints(ints)\u000A fmt.Println(\"Ints: \", ints)\u000A');codeLines.push(' s := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/sorting-by-functions
generated
2
public/sorting-by-functions
generated
@ -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] = 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 := []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] \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/spawning-processes
generated
2
public/spawning-processes
generated
@ -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 := exec.Command(\"date\")\u000A');codeLines.push(' dateOut, err := dateCmd.Output()\u000A if err != nil {\u000A panic(err)\u000A }\u000A fmt.Println(\"\x3E date\")\u000A fmt.Println(string(dateOut))\u000A');codeLines.push(' grepCmd := exec.Command(\"grep\", \"hello\")\u000A');codeLines.push(' grepIn, _ := grepCmd.StdinPipe()\u000A grepOut, _ := grepCmd.StdoutPipe()\u000A grepCmd.Start()\u000A grepIn.Write([]byte(\"hello grep\\ngoodbye grep\"))\u000A grepIn.Close()\u000A grepBytes, _ := ioutil.ReadAll(grepOut)\u000A grepCmd.Wait()\u000A');codeLines.push(' fmt.Println(\"\x3E grep hello\")\u000A fmt.Println(string(grepBytes))\u000A');codeLines.push(' lsCmd := exec.Command(\"bash\", \"-c\", \"ls -a -l -h\")\u000A lsOut, err := lsCmd.Output()\u000A if err != 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/stateful-goroutines
generated
2
public/stateful-goroutines
generated
@ -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 := make(chan readOp)\u000A writes := make(chan writeOp)\u000A');codeLines.push(' go func() {\u000A var state = make(map[int]int)\u000A for {\u000A select {\u000A case read := \x3C-reads:\u000A read.resp \x3C- state[read.key]\u000A case write := \x3C-writes:\u000A state[write.key] = write.val\u000A write.resp \x3C- true\u000A }\u000A }\u000A }()\u000A');codeLines.push(' for r := 0; r \x3C 100; r++ {\u000A go func() {\u000A for {\u000A read := readOp{\u000A key: rand.Intn(5),\u000A resp: make(chan int)}\u000A reads \x3C- read\u000A \x3C-read.resp\u000A atomic.AddUint64(&readOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' for w := 0; w \x3C 10; w++ {\u000A go func() {\u000A for {\u000A write := 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(&writeOps, 1)\u000A time.Sleep(time.Millisecond)\u000A }\u000A }()\u000A }\u000A');codeLines.push(' time.Sleep(time.Second)\u000A');codeLines.push(' readOpsFinal := atomic.LoadUint64(&readOps)\u000A fmt.Println(\"readOps:\", readOpsFinal)\u000A writeOpsFinal := atomic.LoadUint64(&writeOps)\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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/string-formatting
generated
2
public/string-formatting
generated
@ -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 := 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\", &p)\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 := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/string-functions
generated
2
public/string-functions
generated
@ -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 = 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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/structs
generated
2
public/structs
generated
@ -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 := person{name: name}\u000A p.age = 42\u000A return &p\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(&person{name: \"Ann\", age: 40})\u000A');codeLines.push(' fmt.Println(NewPerson(\"Jon\"))\u000A');codeLines.push(' s := person{name: \"Sean\", age: 50}\u000A fmt.Println(s.name)\u000A');codeLines.push(' sp := &s\u000A fmt.Println(sp.age)\u000A');codeLines.push(' sp.age = 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/switch
generated
2
public/switch
generated
@ -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 := 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 := 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 := func(i interface{}) {\u000A switch t := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/temporary-files-and-directories
generated
2
public/temporary-files-and-directories
generated
@ -241,7 +241,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 != nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f, err := 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 = f.Write([]byte{1, 2, 3, 4})\u000A check(err)\u000A');codeLines.push(' dname, err := ioutil.TempDir(\"\", \"sampledir\")\u000A fmt.Println(\"Temp dir name:\", dname)\u000A');codeLines.push(' defer os.RemoveAll(dname)\u000A');codeLines.push(' fname := filepath.Join(dname, \"file1\")\u000A err = 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 !\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 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/testing
generated
2
public/testing
generated
@ -232,7 +232,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 } else {\u000A return b\u000A }\u000A}\u000A');codeLines.push('func TestIntMinBasic(t *testing.T) {\u000A ans := IntMin(2, -2)\u000A if ans != -2 {\u000A');codeLines.push(' t.Errorf(\"IntMin(2, -2) = %d; want -2\", ans)\u000A }\u000A}\u000A');codeLines.push('func TestIntMinTableDriven(t *testing.T) {\u000A var tests = []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 := range tests {\u000A');codeLines.push(' testname := fmt.Sprintf(\"%d,%d\", tt.a, tt.b)\u000A t.Run(testname, func(t *testing.T) {\u000A ans := IntMin(tt.a, tt.b)\u000A if ans != 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 \x3C b {\u000A return a\u000A } else {\u000A return b\u000A }\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/tickers
generated
2
public/tickers
generated
@ -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 := time.NewTicker(500 * time.Millisecond)\u000A done := make(chan bool)\u000A');codeLines.push(' go func() {\u000A for {\u000A select {\u000A case \x3C-done:\u000A return\u000A case t := \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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/time
generated
2
public/time
generated
@ -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 := fmt.Println\u000A');codeLines.push(' now := time.Now()\u000A p(now)\u000A');codeLines.push(' then := 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 := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/time-formatting-parsing
generated
2
public/time-formatting-parsing
generated
@ -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 := fmt.Println\u000A');codeLines.push(' t := time.Now()\u000A p(t.Format(time.RFC3339))\u000A');codeLines.push(' t1, e := 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 := \"3 04 PM\"\u000A t2, e := 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 := \"Mon Jan _2 15:04:05 2006\"\u000A _, e = 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/timeouts
generated
2
public/timeouts
generated
@ -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 := 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 := \x3C-c1:\u000A fmt.Println(res)\u000A case \x3C-time.After(1 * time.Second):\u000A fmt.Println(\"timeout 1\")\u000A }\u000A');codeLines.push(' c2 := make(chan string, 1)\u000A go func() {\u000A time.Sleep(2 * time.Second)\u000A c2 \x3C- \"result 2\"\u000A }()\u000A select {\u000A case res := \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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/timers
generated
2
public/timers
generated
@ -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 := time.NewTimer(2 * time.Second)\u000A');codeLines.push(' \x3C-timer1.C\u000A fmt.Println(\"Timer 1 fired\")\u000A');codeLines.push(' timer2 := time.NewTimer(time.Second)\u000A go func() {\u000A \x3C-timer2.C\u000A fmt.Println(\"Timer 2 fired\")\u000A }()\u000A stop2 := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/url-parsing
generated
2
public/url-parsing
generated
@ -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 := \"postgres://user:pass@host.com:5432/path?k=v#f\"\u000A');codeLines.push(' u, err := url.Parse(s)\u000A if err != 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, _ := u.User.Password()\u000A fmt.Println(p)\u000A');codeLines.push(' fmt.Println(u.Host)\u000A host, port, _ := 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, _ := 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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/values
generated
2
public/values
generated
@ -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 =\", 1+1)\u000A fmt.Println(\"7.0/3.0 =\", 7.0/3.0)\u000A');codeLines.push(' fmt.Println(true && 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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/variables
generated
2
public/variables
generated
@ -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 = \"initial\"\u000A fmt.Println(a)\u000A');codeLines.push(' var b, c int = 1, 2\u000A fmt.Println(b, c)\u000A');codeLines.push(' var d = true\u000A fmt.Println(d)\u000A');codeLines.push(' var e int\u000A fmt.Println(e)\u000A');codeLines.push(' f := \"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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/variadic-functions
generated
2
public/variadic-functions
generated
@ -172,7 +172,7 @@ to form closures, which we’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 := 0\u000A for _, num := range nums {\u000A total += 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 := []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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/waitgroups
generated
2
public/waitgroups
generated
@ -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 := 1; i \x3C= 5; i++ {\u000A wg.Add(1)\u000A go worker(i, &wg)\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 :\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/worker-pools
generated
2
public/worker-pools
generated
@ -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 := 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 = 5\u000A jobs := make(chan int, numJobs)\u000A results := make(chan int, numJobs)\u000A');codeLines.push(' for w := 1; w \x3C= 3; w++ {\u000A go worker(w, jobs, results)\u000A }\u000A');codeLines.push(' for j := 1; j \x3C= numJobs; j++ {\u000A jobs \x3C- j\u000A }\u000A close(jobs)\u000A');codeLines.push(' for a := 1; a \x3C= 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 \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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
2
public/writing-files
generated
2
public/writing-files
generated
@ -287,7 +287,7 @@ we’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 != nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' d1 := []byte(\"hello\\ngo\\n\")\u000A err := ioutil.WriteFile(\"/tmp/dat1\", d1, 0644)\u000A check(err)\u000A');codeLines.push(' f, err := os.Create(\"/tmp/dat2\")\u000A check(err)\u000A');codeLines.push(' defer f.Close()\u000A');codeLines.push(' d2 := []byte{115, 111, 109, 101, 10}\u000A n2, err := f.Write(d2)\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n2)\u000A');codeLines.push(' n3, err := f.WriteString(\"writes\\n\")\u000A fmt.Printf(\"wrote %d bytes\\n\", n3)\u000A');codeLines.push(' f.Sync()\u000A');codeLines.push(' w := bufio.NewWriter(f)\u000A n4, err := w.WriteString(\"buffered\\n\")\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 !\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 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 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
2
public/xml
generated
@ -281,7 +281,7 @@ to nest all <code>plant</code>s under <code><parent><child>...</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=%v, name=%v, origin=%v\",\u000A p.Id, p.Name, p.Origin)\u000A}\u000A');codeLines.push('func main() {\u000A coffee := &Plant{Id: 27, Name: \"Coffee\"}\u000A coffee.Origin = []string{\"Ethiopia\", \"Brazil\"}\u000A');codeLines.push(' out, _ := 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 := xml.Unmarshal(out, &p); err != nil {\u000A panic(err)\u000A }\u000A fmt.Println(p)\u000A');codeLines.push(' tomato := &Plant{Id: 81, Name: \"Tomato\"}\u000A tomato.Origin = []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 := &Nesting{}\u000A nesting.Plants = []*Plant{coffee, tomato}\u000A');codeLines.push(' out, _ = 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\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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user