Merge pull request #274 from mmcgrana/consistent-import
Make multiple imports consistent with import ( ... ) style
This commit is contained in:
commit
65c2883a9f
@ -6,8 +6,10 @@ package main
|
||||
// This syntax imports the `encoding/base64` package with
|
||||
// the `b64` name instead of the default `base64`. It'll
|
||||
// save us some space below.
|
||||
import b64 "encoding/base64"
|
||||
import "fmt"
|
||||
import (
|
||||
b64 "encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
c20da14820b656c867790f2e99bc37140babca8c
|
||||
y_QTcqdkvZh
|
||||
e0148b9b4acb01e849b8f678cba03f549d250c44
|
||||
V3oV1bvh94k
|
||||
|
@ -6,8 +6,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// This is the function we'll run in a goroutine. The
|
||||
// `done` channel will be used to notify another
|
||||
|
@ -1,2 +1,2 @@
|
||||
eb022977181884c2ab0f2b69e50311769e67a509
|
||||
8lmP8beav0p
|
||||
0d5a9de912e2a4a18943e082e2f8107cb75d0ce4
|
||||
fe9If6OhYMk
|
||||
|
@ -18,8 +18,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "strings"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Index returns the first index of the target string `t`, or
|
||||
// -1 if no match is found.
|
||||
|
@ -1,2 +1,2 @@
|
||||
d961fc0e0074aed46cfd1516efdadea78781af56
|
||||
BJB_npWH516
|
||||
28456737ea996664bdaeb8e1e821d95697d00646
|
||||
8hI6oPNEfyh
|
||||
|
@ -5,8 +5,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
41c970a1ef29ad2a05307e6c783ff52ab80eaccd
|
||||
6pFdjf800jj
|
||||
9c80d495201148bbeb0fd0a5a2ce1735aeb34b60
|
||||
myJy_-H8Fo_Q
|
||||
|
@ -8,8 +8,10 @@ package main
|
||||
// Go provides a `flag` package supporting basic
|
||||
// command-line flag parsing. We'll use this package to
|
||||
// implement our example command-line program.
|
||||
import "flag"
|
||||
import "fmt"
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
e2ba0461c090789168c712cc7ed0f66aab09a8c8
|
||||
klFR5DitrCy
|
||||
ab08bf890dcd87b807956b5bee441d59efe458e3
|
||||
lPaZodnG9TF
|
||||
|
@ -3,8 +3,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "math"
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// `const` declares a constant value.
|
||||
const s string = "constant"
|
||||
|
@ -1,2 +1,2 @@
|
||||
2f2ec3a5ff4eef280199da1908eed261346fb40e
|
||||
VhP0f8moZd3
|
||||
7cc09460e8dc6fffd0ba811679f92a85eb51e927
|
||||
gmjHSglwLic
|
||||
|
@ -5,8 +5,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Suppose we wanted to create a file, write to it,
|
||||
// and then close when we're done. Here's how we could
|
||||
|
@ -1,2 +1,2 @@
|
||||
fadbe9c05bb42db672316ba19adf3c2189c7b3f5
|
||||
OrCaBiCrTKq
|
||||
4fc23579a9bd5d8512884902394f4dce51eb7d60
|
||||
QJJ2R6kv6K5
|
||||
|
@ -5,9 +5,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "strings"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
4d0832c5a1ddd4e95474791e8802c15452358214
|
||||
CZJ4R_uu6Uu
|
||||
db2e203da519a22943753295e9cc27d89e4347b0
|
||||
bKuCOOD16KH
|
||||
|
@ -5,8 +5,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
61a498229c8878a97d729cfdd215e5f3960f87ac
|
||||
eN1Qv2ATB-C
|
||||
184c8f3e94dd28176be81956115ac417e33513a6
|
||||
3uYNHHRplmQ
|
||||
|
@ -9,8 +9,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
import "fmt"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// By convention, errors are the last return value and
|
||||
// have type `error`, a built-in interface.
|
||||
|
@ -1,2 +1,2 @@
|
||||
210ba0f8196006c0380acaec01655816848ef168
|
||||
mP_ZR1qjUvA
|
||||
ed58ad3162d93c723d3efe72529a61ce7041fe13
|
||||
vrwN32gxaYx
|
||||
|
@ -10,9 +10,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "syscall"
|
||||
import "os"
|
||||
import "os/exec"
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
b527bbb76a42dd4bae541b73a7377b7e83e79905
|
||||
bf11ADw-2Ho
|
||||
e6b4830d4264f307506b54726ec79b25a0363874
|
||||
ahZjpJaZz44
|
||||
|
@ -3,8 +3,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
dc0bb3eaafa045d6aa05e88aff39322a1ccf822e
|
||||
vDaM0-MGJ_k
|
||||
2316e6c8e364e2066c6bd350dc9b0b2af85b63fe
|
||||
OX997ykuOGx
|
||||
|
@ -3,8 +3,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "math"
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// Here's a basic interface for geometric shapes.
|
||||
type geometry interface {
|
||||
|
@ -1,2 +1,2 @@
|
||||
3547b935d1e0322c0fb696726c27cae53a275e0a
|
||||
0EwsqIn3TTi
|
||||
aac1328f5a04568272b82753ff0762b9eacff4fc
|
||||
hXTlbUAGcvn
|
||||
|
@ -4,9 +4,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "encoding/json"
|
||||
import "fmt"
|
||||
import "os"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// We'll use these two structs to demonstrate encoding and
|
||||
// decoding of custom types below.
|
||||
|
@ -1,2 +1,2 @@
|
||||
6b92694b7be60cdec3e7a04e9fdbf49d5c84adb1
|
||||
63PdbTHxKJA
|
||||
c751bc7223b8bc615f82fe7643ab98ce2b80240f
|
||||
ROikmz5tRhZ
|
||||
|
@ -5,8 +5,10 @@ package main
|
||||
|
||||
// The built-in package `strconv` provides the number
|
||||
// parsing.
|
||||
import "strconv"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
0d2155e9863a73c098d44637e92403d7f5e8e965
|
||||
NZh4LjhguvN
|
||||
0191c7e43706640207c403ba92dd2272d66fc868
|
||||
t2q4KnWWTAw
|
||||
|
@ -4,9 +4,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
import "math/rand"
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
8e97de760147b061dd09939db294c892211b6b80
|
||||
jiJaIjxL2sP
|
||||
9374869a809d28ea784a9e1181b4aa1988018776
|
||||
DVHO7SjJZnp
|
||||
|
@ -6,8 +6,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
edad78bf3b36ddc9bec30b344b8a72be4de90f3b
|
||||
l4uDE-RCDpa
|
||||
357d83df3e48675eb1e135188cb9f07448c1f146
|
||||
AJ-MJephNib
|
||||
|
@ -4,9 +4,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "bytes"
|
||||
import "fmt"
|
||||
import "regexp"
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
7cde6b9af5cf6c47606001dd54eee468a6c61dbb
|
||||
qR5gn2l0AGa
|
||||
de24265897edf1d3913e3b87f70757284a66ecea
|
||||
urHlUNDVenk
|
||||
|
@ -4,8 +4,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
8d743edffd7de6bf7bccdf4437f45672b6adc75e
|
||||
ZdSOPe1Gj13
|
||||
6e1125087bc036ebd905452300575f160d683918
|
||||
yF-xgN7Xf9P
|
||||
|
@ -9,8 +9,10 @@ package main
|
||||
|
||||
// Go implements several hash functions in various
|
||||
// `crypto/*` packages.
|
||||
import "crypto/sha1"
|
||||
import "fmt"
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := "sha1 this string"
|
||||
|
@ -1,2 +1,2 @@
|
||||
6a896270e34f2696b881a8fa7e68bfff57dee51f
|
||||
1oT-5GBUkLr
|
||||
4cda643ba233014fe6b30966c37d4d0fcd4edbe8
|
||||
oqcrTfY4Ykd
|
||||
|
@ -7,10 +7,12 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
import "os/signal"
|
||||
import "syscall"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
9720d747e3ab2893df508a70cbb341c90fdd7ca1
|
||||
9koJAW1raI5
|
||||
1e43c6f63f1d57e1a52c89f52d35b68757e9676b
|
||||
_6oj-T3Gko2
|
||||
|
@ -6,8 +6,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "sort"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// In order to sort by a custom function in Go, we need a
|
||||
// corresponding type. Here we've created a `byLength`
|
||||
|
@ -1,2 +1,2 @@
|
||||
6a04058b564d5741815e523f97f240ee6563cb15
|
||||
y3kuCwIFRYK
|
||||
f7d0b7840dd12601fb86946f9dc4c38fb1c0501f
|
||||
Jtxf94x94Hx
|
||||
|
@ -4,8 +4,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "sort"
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
4e576421f2bdbd11847c367d223bd30d0e301990
|
||||
e6hp3Rn-oH6
|
||||
e11e944d34b21e75ce4f7c91026d4200ce592dc5
|
||||
tAWAkRlBJNX
|
||||
|
@ -7,9 +7,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "io/ioutil"
|
||||
import "os/exec"
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
0b676b93e41ac5434003c194bc038d5f3ce76bc8
|
||||
6HRWVK5gPYU
|
||||
cc68e4290f10209ad2fa8db74fdfaea7fdb44d5c
|
||||
QS_Nkoe8VLG
|
||||
|
@ -4,8 +4,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type point struct {
|
||||
x, y int
|
||||
|
@ -1,2 +1,2 @@
|
||||
5f39ae6d8f26d59a688a9a9d7d13a5c1d0f7a08b
|
||||
CkBQ3CFpHQ9
|
||||
12b245c576b43537c092a5b84995ebca8ce78a57
|
||||
vmYSdxfUcRh
|
||||
|
@ -4,8 +4,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import s "strings"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
s "strings"
|
||||
)
|
||||
|
||||
// We alias `fmt.Println` to a shorter name as we'll use
|
||||
// it a lot below.
|
||||
|
@ -1,2 +1,2 @@
|
||||
17aa523bbd606fa0b624fae44b89812d46330755
|
||||
Vn4D3y4_711
|
||||
bf39c7540bd78eba38eb5a9047a9d0ffc7235f85
|
||||
xoRUhG86wsF
|
||||
|
@ -3,8 +3,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
b47004b3e3b6d787ea98642dc5b955df57cd2bcd
|
||||
TJ4Az0KuLfL
|
||||
2486fc553301cdeac9a26f3d0b3aed4143d9f4f0
|
||||
ZcDzdx3nYQn
|
||||
|
@ -6,8 +6,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
c83f34821c69d156713919a42c73ec9f58560f72
|
||||
IUmkvvXL5Ok
|
||||
4a42333d14f902e890902343c7bd9b9c735fd8ad
|
||||
n1q1sSGEvmv
|
||||
|
@ -3,8 +3,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
p := fmt.Println
|
||||
|
@ -1,2 +1,2 @@
|
||||
1f9962260f5c92efe57db0b96099b3dd06c90333
|
||||
nHAisH6amZG
|
||||
9e3f17061fef280191e3e8518365e231e17a5d5a
|
||||
1410R7Fcyx0
|
||||
|
@ -3,8 +3,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
p := fmt.Println
|
||||
|
@ -1,2 +1,2 @@
|
||||
b6308f1fea7665e89a28f54aac6cb49b95685eb5
|
||||
PZMCzzaJURJ
|
||||
c47d853fa7527a652ce78b0285e452c6cd740050
|
||||
u-7i_p8BHVt
|
||||
|
@ -5,8 +5,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
b1e8d0efbabd0c52271a85fad5ad58dcd1c7c476
|
||||
gyY_qDsRVUe
|
||||
b8d3e745539b24d3530ca21efcdc924f08769edb
|
||||
TYJgoFjlTd6
|
||||
|
@ -6,8 +6,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
e10c601ab3b702dfcea728c1edb31673561484b5
|
||||
pybl9hRvJq2
|
||||
e8e501d6083bea786629ca5e485e8b18caab4815
|
||||
pLnKEIesooU
|
||||
|
@ -3,9 +3,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "net"
|
||||
import "net/url"
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
b7a0813e9413bfcc956cc58b850f655dd129ebb7
|
||||
AL79Lv-9CWo
|
||||
babc12f5066652f4cb0151231c06f1037298ff28
|
||||
M218D9Tldlr
|
||||
|
@ -3,8 +3,10 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Here's the worker, of which we'll run several
|
||||
// concurrent instances. These workers will receive
|
||||
|
@ -1,2 +1,2 @@
|
||||
bc69c6602d438413dcb9ceac112299ee253e4575
|
||||
yuHsGf712D1
|
||||
9b30cdfc3f46d634c3b8671a7ae1551c133fb6e2
|
||||
IiKZ-nj-nKY
|
||||
|
10
public/base64-encoding
generated
10
public/base64-encoding
generated
@ -42,7 +42,7 @@ encoding/decoding</a>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/y_QTcqdkvZh"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/V3oV1bvh94k"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -58,8 +58,10 @@ save us some space below.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="nx">b64</span> <span class="s">"encoding/base64"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="nx">b64</span> <span class="s">"encoding/base64"</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -189,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 b64 \"encoding/base64\"\u000Aimport \"fmt\"\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 := \"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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/channel-synchronization
generated
10
public/channel-synchronization
generated
@ -45,7 +45,7 @@ you may prefer to use a <a href="waitgroups">WaitGroup</a>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/8lmP8beav0p"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/fe9If6OhYMk"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -58,8 +58,10 @@ you may prefer to use a <a href="waitgroups">WaitGroup</a>.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"time"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -182,7 +184,7 @@ started.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"time\"\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 := 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>
|
||||
|
10
public/collection-functions
generated
10
public/collection-functions
generated
@ -75,7 +75,7 @@ helper function.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/BJB_npWH516"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/8hI6oPNEfyh"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -88,8 +88,10 @@ helper function.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"strings"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"strings"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -369,7 +371,7 @@ type.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"strings\"\u000Aimport \"fmt\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/command-line-arguments
generated
10
public/command-line-arguments
generated
@ -44,7 +44,7 @@ For example, <code>go run hello.go</code> uses <code>run</code> and
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/6pFdjf800jj"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/myJy_-H8Fo_Q"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -57,8 +57,10 @@ For example, <code>go run hello.go</code> uses <code>run</code> and
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"os"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"os"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -168,7 +170,7 @@ with flags.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"os\"\u000Aimport \"fmt\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/command-line-flags
generated
10
public/command-line-flags
generated
@ -44,7 +44,7 @@ command-line flag.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/klFR5DitrCy"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/lPaZodnG9TF"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -60,8 +60,10 @@ implement our example command-line program.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"flag"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"flag"</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -308,7 +310,7 @@ and show the help text again.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"flag\"\u000Aimport \"fmt\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/constants
generated
10
public/constants
generated
@ -42,7 +42,7 @@ and numeric values.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/VhP0f8moZd3"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/gmjHSglwLic"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -55,8 +55,10 @@ and numeric values.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"math"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"math"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -181,7 +183,7 @@ assignment or function call. For example, here
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"math\"\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 = \"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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/defer
generated
10
public/defer
generated
@ -44,7 +44,7 @@ purposes of cleanup. <code>defer</code> is often used where e.g.
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/OrCaBiCrTKq"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/QJJ2R6kv6K5"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -57,8 +57,10 @@ purposes of cleanup. <code>defer</code> is often used where e.g.
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"os"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"os"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -210,7 +212,7 @@ after being written.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"os\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
12
public/environment-variables
generated
12
public/environment-variables
generated
@ -44,7 +44,7 @@ Let’s look at how to set, get, and list environment variables.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/CZJ4R_uu6Uu"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/bKuCOOD16KH"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -57,9 +57,11 @@ Let’s look at how to set, get, and list environment variables.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"os"</span>
|
||||
<span class="kn">import</span> <span class="s">"strings"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"os"</span>
|
||||
<span class="s">"strings"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -184,7 +186,7 @@ program picks that value up.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"os\"\u000Aimport \"strings\"\u000Aimport \"fmt\"\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.Split(e, \"=\")\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 := range os.Environ() {\u000A pair := strings.Split(e, \"=\")\u000A fmt.Println(pair[0])\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/epoch
generated
10
public/epoch
generated
@ -44,7 +44,7 @@ Here’s how to do it in Go.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/eN1Qv2ATB-C"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/3uYNHHRplmQ"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -57,8 +57,10 @@ Here’s how to do it in Go.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"time"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -175,7 +177,7 @@ parsing and formatting.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"time\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/errors
generated
10
public/errors
generated
@ -48,7 +48,7 @@ non-error tasks.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/mP_ZR1qjUvA"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/vrwN32gxaYx"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -61,8 +61,10 @@ non-error tasks.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"errors"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"errors"</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -297,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 \"errors\"\u000Aimport \"fmt\"\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 == 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
12
public/execing-processes
generated
12
public/execing-processes
generated
@ -49,7 +49,7 @@ function.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/bf11ADw-2Ho"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/ahZjpJaZz44"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -62,9 +62,11 @@ function.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"syscall"</span>
|
||||
<span class="kn">import</span> <span class="s">"os"</span>
|
||||
<span class="kn">import</span> <span class="s">"os/exec"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"os"</span>
|
||||
<span class="s">"os/exec"</span>
|
||||
<span class="s">"syscall"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -201,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 \"syscall\"\u000Aimport \"os\"\u000Aimport \"os/exec\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/exit
generated
10
public/exit
generated
@ -38,7 +38,7 @@ status.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/vDaM0-MGJ_k"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/OX997ykuOGx"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -51,8 +51,10 @@ status.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"os"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"os"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -168,7 +170,7 @@ the status in the terminal.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"os\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' defer fmt.Println(\"!\")\u000A');codeLines.push(' os.Exit(3)\u000A}\u000A');codeLines.push('');codeLines.push('');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(' defer fmt.Println(\"!\")\u000A');codeLines.push(' os.Exit(3)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/interfaces
generated
10
public/interfaces
generated
@ -42,7 +42,7 @@ signatures.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/0EwsqIn3TTi"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/hXTlbUAGcvn"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -55,8 +55,10 @@ signatures.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"math"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"math"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -234,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 \"fmt\"\u000Aimport \"math\"\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 := rect{width: 3, height: 4}\u000A c := 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>
|
||||
|
12
public/json
generated
12
public/json
generated
@ -43,7 +43,7 @@ data types.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/63PdbTHxKJA"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/ROikmz5tRhZ"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -56,9 +56,11 @@ data types.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"encoding/json"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"os"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"encoding/json"</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"os"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -414,7 +416,7 @@ for more.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"encoding/json\"\u000Aimport \"fmt\"\u000Aimport \"os\"\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, _ := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/number-parsing
generated
10
public/number-parsing
generated
@ -42,7 +42,7 @@ in many programs; here’s how to do it in Go.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/NZh4LjhguvN"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/t2q4KnWWTAw"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -57,8 +57,10 @@ parsing.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"strconv"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"strconv"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -211,7 +213,7 @@ bits.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"strconv\"\u000Aimport \"fmt\"\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, _ := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
12
public/random-numbers
generated
12
public/random-numbers
generated
@ -43,7 +43,7 @@ generation.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/jiJaIjxL2sP"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/DVHO7SjJZnp"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -56,9 +56,11 @@ generation.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"time"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"math/rand"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"math/rand"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -227,7 +229,7 @@ that Go can provide.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"time\"\u000Aimport \"fmt\"\u000Aimport \"math/rand\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/rate-limiting
generated
10
public/rate-limiting
generated
@ -45,7 +45,7 @@ channels, and <a href="tickers">tickers</a>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/l4uDE-RCDpa"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/AJ-MJephNib"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -58,8 +58,10 @@ channels, and <a href="tickers">tickers</a>.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"time"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -259,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 \"time\"\u000Aimport \"fmt\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
12
public/regular-expressions
generated
12
public/regular-expressions
generated
@ -43,7 +43,7 @@ in Go.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/qR5gn2l0AGa"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/urHlUNDVenk"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -56,9 +56,11 @@ in Go.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"bytes"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"regexp"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"bytes"</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"regexp"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -340,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 \"bytes\"\u000Aimport \"fmt\"\u000Aimport \"regexp\"\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, _ := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/select
generated
10
public/select
generated
@ -43,7 +43,7 @@ select is a powerful feature of Go.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/ZdSOPe1Gj13"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/yF-xgN7Xf9P"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -56,8 +56,10 @@ select is a powerful feature of Go.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"time"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -181,7 +183,7 @@ concurrently.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"time\"\u000Aimport \"fmt\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/sha1-hashes
generated
10
public/sha1-hashes
generated
@ -46,7 +46,7 @@ compute SHA1 hashes in Go.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/1oT-5GBUkLr"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/oqcrTfY4Ykd"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -61,8 +61,10 @@ compute SHA1 hashes in Go.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"crypto/sha1"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"crypto/sha1"</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -201,7 +203,7 @@ you should carefully research
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"crypto/sha1\"\u000Aimport \"fmt\"\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 := \"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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
14
public/signals
generated
14
public/signals
generated
@ -46,7 +46,7 @@ Here’s how to handle signals in Go with channels.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/9koJAW1raI5"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/_6oj-T3Gko2"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -59,10 +59,12 @@ Here’s how to handle signals in Go with channels.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"os"</span>
|
||||
<span class="kn">import</span> <span class="s">"os/signal"</span>
|
||||
<span class="kn">import</span> <span class="s">"syscall"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"os"</span>
|
||||
<span class="s">"os/signal"</span>
|
||||
<span class="s">"syscall"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -186,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 \"fmt\"\u000Aimport \"os\"\u000Aimport \"os/signal\"\u000Aimport \"syscall\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/sorting
generated
10
public/sorting
generated
@ -43,7 +43,7 @@ builtins first.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/e6hp3Rn-oH6"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/tAWAkRlBJNX"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -56,8 +56,10 @@ builtins first.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"sort"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"sort"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -158,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 \"fmt\"\u000Aimport \"sort\"\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 := []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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/sorting-by-functions
generated
10
public/sorting-by-functions
generated
@ -45,7 +45,7 @@ in Go.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/y3kuCwIFRYK"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/Jtxf94x94Hx"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -58,8 +58,10 @@ in Go.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"sort"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"sort"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -175,7 +177,7 @@ functions.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"sort\"\u000Aimport \"fmt\"\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] = 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
12
public/spawning-processes
generated
12
public/spawning-processes
generated
@ -46,7 +46,7 @@ of spawning processes from Go.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/6HRWVK5gPYU"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/QS_Nkoe8VLG"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -59,9 +59,11 @@ of spawning processes from Go.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"io/ioutil"</span>
|
||||
<span class="kn">import</span> <span class="s">"os/exec"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"io/ioutil"</span>
|
||||
<span class="s">"os/exec"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -257,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 \"fmt\"\u000Aimport \"io/ioutil\"\u000Aimport \"os/exec\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/string-formatting
generated
10
public/string-formatting
generated
@ -43,7 +43,7 @@ common string formatting tasks.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/CkBQ3CFpHQ9"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/vmYSdxfUcRh"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -56,8 +56,10 @@ common string formatting tasks.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"os"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"os"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -455,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 \"fmt\"\u000Aimport \"os\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/string-functions
generated
10
public/string-functions
generated
@ -43,7 +43,7 @@ to give you a sense of the package.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/Vn4D3y4_711"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/xoRUhG86wsF"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -56,8 +56,10 @@ to give you a sense of the package.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="nx">s</span> <span class="s">"strings"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="nx">s</span> <span class="s">"strings"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -207,7 +209,7 @@ for more information.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import s \"strings\"\u000Aimport \"fmt\"\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 = 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>
|
||||
|
10
public/switch
generated
10
public/switch
generated
@ -42,7 +42,7 @@ branches.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/TJ4Az0KuLfL"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/ZcDzdx3nYQn"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -55,8 +55,10 @@ branches.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"time"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -201,7 +203,7 @@ type corresponding to its clause.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"time\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/tickers
generated
10
public/tickers
generated
@ -45,7 +45,7 @@ periodically until we stop it.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/IUmkvvXL5Ok"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/n1q1sSGEvmv"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -58,8 +58,10 @@ periodically until we stop it.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"time"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -169,7 +171,7 @@ before we stop it.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"time\"\u000Aimport \"fmt\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/time
generated
10
public/time
generated
@ -42,7 +42,7 @@ here are some examples.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/PZMCzzaJURJ"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/u-7i_p8BHVt"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -55,8 +55,10 @@ here are some examples.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"time"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -268,7 +270,7 @@ the Unix epoch.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"time\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/time-formatting-parsing
generated
10
public/time-formatting-parsing
generated
@ -42,7 +42,7 @@ pattern-based layouts.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/nHAisH6amZG"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/1410R7Fcyx0"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -55,8 +55,10 @@ pattern-based layouts.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<span class="kn">import</span> <span class="s">"time"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -202,7 +204,7 @@ explaining the parsing problem.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000Aimport \"time\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/timeouts
generated
10
public/timeouts
generated
@ -44,7 +44,7 @@ elegant thanks to channels and <code>select</code>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/gyY_qDsRVUe"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/TYJgoFjlTd6"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -57,8 +57,10 @@ elegant thanks to channels and <code>select</code>.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"time"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -179,7 +181,7 @@ out and the second succeeding.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"time\"\u000Aimport \"fmt\"\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 := 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('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
10
public/timers
generated
10
public/timers
generated
@ -45,7 +45,7 @@ at <a href="tickers">tickers</a>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/pybl9hRvJq2"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/pLnKEIesooU"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -58,8 +58,10 @@ at <a href="tickers">tickers</a>.</p>
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"time"</span>
|
||||
<span class="kn">import</span> <span class="s">"fmt"</span>
|
||||
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="s">"fmt"</span>
|
||||
<span class="s">"time"</span>
|
||||
<span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -168,7 +170,7 @@ a chance to expire.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"time\"\u000Aimport \"fmt\"\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 expired\")\u000A');codeLines.push(' timer2 := time.NewTimer(time.Second)\u000A go func() {\u000A \x3C-timer2.C\u000A fmt.Println(\"Timer 2 expired\")\u000A }()\u000A stop2 := timer2.Stop()\u000A if stop2 {\u000A fmt.Println(\"Timer 2 stopped\")\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(' timer1 := time.NewTimer(2 * time.Second)\u000A');codeLines.push(' \x3C-timer1.C\u000A fmt.Println(\"Timer 1 expired\")\u000A');codeLines.push(' timer2 := time.NewTimer(time.Second)\u000A go func() {\u000A \x3C-timer2.C\u000A fmt.Println(\"Timer 2 expired\")\u000A }()\u000A stop2 := timer2.Stop()\u000A if stop2 {\u000A fmt.Println(\"Timer 2 stopped\")\u000A }\u000A}\u000A');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user