mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:21:38 +08:00
fix: coding style and file format for all example.
This commit is contained in:
@@ -37,6 +37,7 @@ func (l *Log) String() string {
|
||||
func (c *Customer) Log() *Log {
|
||||
return c.log
|
||||
}
|
||||
|
||||
/* Output:
|
||||
1 - Yes we can!
|
||||
2 - After me the world will be a better place!
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./struct_pack/structPack"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -12,5 +12,6 @@ func main() {
|
||||
fmt.Printf("Mi1 = %d\n", struct1.Mi1)
|
||||
fmt.Printf("Mf1 = %f\n", struct1.Mf1)
|
||||
}
|
||||
|
||||
// Mi1 = 10
|
||||
// Mf1 = 16.000000
|
@@ -19,6 +19,7 @@ func main() {
|
||||
fmt.Println("Full time now:", m.String()) //calling existing String method on anonymous Time field
|
||||
fmt.Println("First 3 chars:", m.first3Chars()) //calling myTime.first3Chars
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Full time now: Mon Oct 24 15:34:54 Romance Daylight Time 2011
|
||||
First 3 chars: Mon
|
||||
|
@@ -23,5 +23,3 @@ func main() {
|
||||
func (tn *TwoInts) String() string {
|
||||
return "(" + strconv.Itoa(tn.a) + " / " + strconv.Itoa(tn.b) + ")"
|
||||
}
|
||||
|
||||
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type List []int
|
||||
|
||||
func (l List) Len() int { return len(l) }
|
||||
func (l *List) Append(val int) { *l = append(*l, val) }
|
||||
|
||||
|
@@ -27,6 +27,7 @@ func main() {
|
||||
fmt.Println("It exhibits behavior of a Camera: ", cp.TakeAPicture())
|
||||
fmt.Println("It works like a Phone too: ", cp.Call())
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Our new CameraPhone exhibits multiple behaviors ...
|
||||
It exhibits behavior of a Camera: Click
|
||||
|
@@ -34,6 +34,7 @@ func main() {
|
||||
upPerson(pers3)
|
||||
fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The name of the person is CHRIS WOODWARD
|
||||
The name of the person is CHRIS WOODWARD
|
||||
|
@@ -12,6 +12,3 @@ func (p *Person) FirstName() string {
|
||||
func (p *Person) SetFirstName(newName string) {
|
||||
p.firstName = newName
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -22,6 +22,7 @@ func main() {
|
||||
b2.change()
|
||||
fmt.Println(b2.write())
|
||||
}
|
||||
|
||||
/* Output:
|
||||
{1}
|
||||
{1}
|
||||
|
@@ -21,4 +21,5 @@ func main() {
|
||||
var c = number(b)
|
||||
fmt.Println(a, b, c)
|
||||
}
|
||||
|
||||
// output: {5} {5} {5}
|
||||
|
@@ -23,6 +23,7 @@ func refTag(tt TagType, ix int) {
|
||||
ixField := ttType.Field(ix)
|
||||
fmt.Printf("%v\n", ixField.Tag)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
An important answer
|
||||
The name of the thing
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./person"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
|
||||
type Any interface{}
|
||||
type Car struct {
|
||||
Model string
|
||||
|
@@ -29,4 +29,5 @@ func TypeSwitch() {
|
||||
func main() {
|
||||
TypeSwitch()
|
||||
}
|
||||
|
||||
// Output: any hello is a special String!
|
||||
|
@@ -27,4 +27,5 @@ func main() {
|
||||
areaIntf := sq1
|
||||
fmt.Printf("The square has area: %f\n", areaIntf.Area())
|
||||
}
|
||||
|
||||
// The square has area: 25.000000
|
||||
|
@@ -28,11 +28,12 @@ func main() {
|
||||
q := &Square{5} // Area() of Square needs a pointer
|
||||
shapes := []Shaper{r, q}
|
||||
fmt.Println("Looping through shapes for area ...")
|
||||
for n, _ := range shapes {
|
||||
for n := range shapes {
|
||||
fmt.Println("Shape details: ", shapes[n])
|
||||
fmt.Println("Area of this shape is: ", shapes[n].Area())
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Looping through shapes for area ...
|
||||
Shape details: {5 3}
|
||||
@@ -40,6 +41,3 @@ Area of this shape is: 15
|
||||
Shape details: &{5}
|
||||
Area of this shape is: 25
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type List []int
|
||||
|
||||
func (l List) Len() int { return len(l) }
|
||||
func (l *List) Append(val int) { *l = append(*l, val) }
|
||||
|
||||
|
@@ -40,17 +40,18 @@ func main() {
|
||||
q := &Square{5} // Area() of Square needs a pointer
|
||||
shapes := []Shaper{r, q}
|
||||
fmt.Println("Looping through shapes for area ...")
|
||||
for n, _ := range shapes {
|
||||
for n := range shapes {
|
||||
fmt.Println("Shape details: ", shapes[n])
|
||||
fmt.Println("Area of this shape is: ", shapes[n].Area())
|
||||
}
|
||||
topgen := []TopologicalGenus{r, q}
|
||||
fmt.Println("Looping through topgen for rank ...")
|
||||
for n, _ := range topgen {
|
||||
for n := range topgen {
|
||||
fmt.Println("Shape details: ", topgen[n])
|
||||
fmt.Println("Topological Genus of this shape is: ", topgen[n].Rank())
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Looping through shapes for area ...
|
||||
Shape details: {5 3}
|
||||
|
@@ -26,13 +26,19 @@ func (day Day) String() string {
|
||||
|
||||
func print(args ...interface{}) {
|
||||
for i, arg := range args {
|
||||
if i > 0 {os.Stdout.WriteString(" ")}
|
||||
if i > 0 {
|
||||
os.Stdout.WriteString(" ")
|
||||
}
|
||||
switch a := arg.(type) { // type switch
|
||||
case Stringer: os.Stdout.WriteString(a.String())
|
||||
case int: os.Stdout.WriteString(strconv.Itoa(a))
|
||||
case string: os.Stdout.WriteString(a)
|
||||
case Stringer:
|
||||
os.Stdout.WriteString(a.String())
|
||||
case int:
|
||||
os.Stdout.WriteString(strconv.Itoa(a))
|
||||
case string:
|
||||
os.Stdout.WriteString(a)
|
||||
// more types
|
||||
default: os.Stdout.WriteString("???")
|
||||
default:
|
||||
os.Stdout.WriteString("???")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,4 +46,5 @@ func print(args ...interface{}) {
|
||||
func main() {
|
||||
print(Day(1), "was", Celsius(18.36)) // Tuesday was 18.4 °C
|
||||
}
|
||||
|
||||
// Tuesday was 18.4 °C
|
@@ -20,6 +20,7 @@ func main() {
|
||||
y := v.Interface().(float64)
|
||||
fmt.Println(y)
|
||||
}
|
||||
|
||||
/* output:
|
||||
type: float64
|
||||
value: <float64 Value>
|
||||
|
@@ -22,6 +22,7 @@ func main() {
|
||||
fmt.Println(v.Interface())
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
settability of v: false
|
||||
type of v: *float64
|
||||
|
@@ -37,6 +37,7 @@ func main() {
|
||||
results := value.Method(0).Call(nil)
|
||||
fmt.Println(results) // [Ada - Go - Oberon]
|
||||
}
|
||||
|
||||
/* Output:
|
||||
main.NotknownType
|
||||
struct
|
||||
|
@@ -24,6 +24,7 @@ func main() {
|
||||
s.Field(1).SetString("Sunset Strip")
|
||||
fmt.Println("t is now", t)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
0: A int = 23
|
||||
1: B string = skidoo
|
||||
|
@@ -39,11 +39,13 @@ func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
type Float64Array []float64
|
||||
|
||||
func (p Float64Array) Len() int { return len(p) }
|
||||
func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
type StringArray []string
|
||||
|
||||
func (p StringArray) Len() int { return len(p) }
|
||||
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
|
||||
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
@@ -6,8 +6,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./sort"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// sorting of slice of integers
|
||||
@@ -68,7 +68,6 @@ func days() {
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
ints()
|
||||
strings()
|
||||
|
@@ -2,11 +2,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var r io.Reader
|
||||
|
@@ -43,8 +43,8 @@ package cgl
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"reflect"
|
||||
|
@@ -1,11 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"io"
|
||||
"fmt"
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func cat(r *bufio.Reader) {
|
||||
@@ -33,4 +33,3 @@ func main() {
|
||||
cat(bufio.NewReader(f))
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"flag" // command line option parser
|
||||
"os"
|
||||
)
|
||||
|
||||
var NewLine = flag.Bool("n", false, "print newline") // echo -n flag, of type *bool
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@@ -3,8 +3,8 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
@@ -38,4 +38,5 @@ func main() {
|
||||
}
|
||||
fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y)
|
||||
}
|
||||
|
||||
// Output: "Pythagoras": {3,4}
|
||||
|
@@ -36,4 +36,3 @@ func main() {
|
||||
log.Println("Error in encoding gob")
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -2,10 +2,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bufio"
|
||||
"os"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@@ -2,8 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
@@ -24,6 +24,7 @@ func main() {
|
||||
checksum := hasher.Sum(b)
|
||||
fmt.Printf("Result: %x\n", checksum)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Result: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
|
||||
Result: [169 74 143 229 204 177 155 166 28 76 8 115 211 145 233 135 152 47 187 211]
|
||||
|
@@ -2,8 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
@@ -32,6 +32,7 @@ func main() {
|
||||
fmt.Printf("From XML: %#v\n", tx)
|
||||
|
||||
}
|
||||
|
||||
/* Output with
|
||||
type thing struct {
|
||||
Field1 int
|
||||
|
@@ -31,6 +31,7 @@ func main() {
|
||||
fmt.Println(col2)
|
||||
fmt.Println(col3)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
[ABC FUNC GO]
|
||||
[40 56 45]
|
||||
|
@@ -21,9 +21,7 @@ func main() {
|
||||
break
|
||||
}
|
||||
r := bufio.NewReader(fin)
|
||||
for line, _, err := r.ReadLine();
|
||||
err != io.EOF;
|
||||
line, _, err = r.ReadLine() {
|
||||
for line, _, err := r.ReadLine(); err != io.EOF; line, _, err = r.ReadLine() {
|
||||
fmt.Printf("Lines: %v (error %v)\n", string(line), err)
|
||||
}
|
||||
}
|
||||
|
@@ -2,8 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -19,23 +19,33 @@ func main() {
|
||||
fmt.Printf("Your name is %s", input)
|
||||
// For Unix: test with delimiter "\n", for Windows: test with "\r\n"
|
||||
switch input {
|
||||
case "Philip\r\n": fmt.Println("Welcome Philip!")
|
||||
case "Chris\r\n": fmt.Println("Welcome Chris!")
|
||||
case "Ivo\r\n": fmt.Println("Welcome Ivo!")
|
||||
default: fmt.Printf("You are not welcome here! Goodbye!")
|
||||
case "Philip\r\n":
|
||||
fmt.Println("Welcome Philip!")
|
||||
case "Chris\r\n":
|
||||
fmt.Println("Welcome Chris!")
|
||||
case "Ivo\r\n":
|
||||
fmt.Println("Welcome Ivo!")
|
||||
default:
|
||||
fmt.Printf("You are not welcome here! Goodbye!")
|
||||
}
|
||||
|
||||
// version 2:
|
||||
switch input {
|
||||
case "Philip\r\n": fallthrough
|
||||
case "Ivo\r\n": fallthrough
|
||||
case "Chris\r\n": fmt.Printf("Welcome %s\n", input)
|
||||
default: fmt.Printf("You are not welcome here! Goodbye!\n")
|
||||
case "Philip\r\n":
|
||||
fallthrough
|
||||
case "Ivo\r\n":
|
||||
fallthrough
|
||||
case "Chris\r\n":
|
||||
fmt.Printf("Welcome %s\n", input)
|
||||
default:
|
||||
fmt.Printf("You are not welcome here! Goodbye!\n")
|
||||
}
|
||||
|
||||
// version 3:
|
||||
switch input {
|
||||
case "Philip\r\n", "Ivo\r\n": fmt.Printf("Welcome %s\n", input)
|
||||
default: fmt.Printf("You are not welcome here! Goodbye!\n")
|
||||
case "Philip\r\n", "Ivo\r\n":
|
||||
fmt.Printf("Welcome %s\n", input)
|
||||
default:
|
||||
fmt.Printf("You are not welcome here! Goodbye!\n")
|
||||
}
|
||||
}
|
||||
|
@@ -2,9 +2,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"strings"
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
var t, token xml.Token
|
||||
@@ -37,6 +37,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Token name: Person
|
||||
Token name: FirstName
|
||||
|
@@ -11,4 +11,5 @@ var errNotFound error = errors.New("Not found error")
|
||||
func main() {
|
||||
fmt.Printf("error: %v", errNotFound)
|
||||
}
|
||||
|
||||
// error: Not found error
|
@@ -2,8 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"even/even"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@@ -1,9 +1,10 @@
|
||||
// exec.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -56,6 +57,5 @@ The process id is &{2054 0}total 2056
|
||||
fmt.Printf("The command is %v", cmd)
|
||||
// The command is &{/bin/ls [ls -l] [] <nil> <nil> <nil> 0xf840000210 <nil> true [0xf84000ea50 0xf84000e9f0 0xf84000e9c0] [0xf84000ea50 0xf84000e9f0 0xf84000e9c0] [] [] 0xf8400128c0}
|
||||
}
|
||||
|
||||
// in Windows: uitvoering: Error fork/exec /bin/ls: The system cannot find the path specified. starting process!
|
||||
|
||||
|
||||
|
@@ -2,8 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./parse/parse"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -25,6 +25,7 @@ func main() {
|
||||
fmt.Println(nums)
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Parsing "1 2 3 4 5":
|
||||
[1 2 3 4 5]
|
||||
|
@@ -3,8 +3,8 @@ package parse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A ParseError indicates an error in converting a word into an integer.
|
||||
|
@@ -18,7 +18,7 @@ func BenchmarkChannelSync(b *testing.B) {
|
||||
}
|
||||
close(ch)
|
||||
}()
|
||||
for _ = range ch {
|
||||
for range ch {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,6 @@ func BenchmarkChannelBuffered(b *testing.B) {
|
||||
}
|
||||
close(ch)
|
||||
}()
|
||||
for _ = range ch {
|
||||
for range ch {
|
||||
}
|
||||
}
|
||||
|
@@ -13,4 +13,3 @@ func pump(ch chan int) {
|
||||
ch <- i
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -47,6 +47,7 @@ func main() {
|
||||
fmt.Println("Salary changed:")
|
||||
fmt.Println(bs)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Person - name is: Smith Bill - salary is: 2500.50
|
||||
Salary changed:
|
||||
|
@@ -43,6 +43,7 @@ func BuildLazyIntEvaluator(evalFunc EvalFunc, initState Any) func() int {
|
||||
return ef().(int)
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
0th even: 0
|
||||
1th even: 2
|
||||
|
@@ -30,4 +30,5 @@ func getData(ch chan string) {
|
||||
fmt.Printf("%s ", input)
|
||||
}
|
||||
}
|
||||
|
||||
// Washington Tripoli London Beijing Tokio
|
@@ -26,4 +26,5 @@ func getData(ch chan string) {
|
||||
fmt.Printf("%s ", input)
|
||||
}
|
||||
}
|
||||
|
||||
// Washington Tripoli London Beijing Tokio
|
@@ -2,8 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -45,5 +45,3 @@ func suck(ch1,ch2 chan int) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
const MAXREQS = 50
|
||||
|
||||
var sem = make(chan int, MAXREQS)
|
||||
|
||||
type Request struct {
|
||||
|
@@ -22,6 +22,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
.
|
||||
.
|
||||
|
@@ -39,6 +39,7 @@ func main() {
|
||||
}
|
||||
time.Sleep(1e9)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
0 1 2 3 4
|
||||
4 4 4 4 4
|
||||
|
@@ -9,7 +9,6 @@ type nexter interface {
|
||||
next() byte
|
||||
}
|
||||
|
||||
|
||||
func nextFew1(n nexter, num int) []byte {
|
||||
var b []byte
|
||||
for i := 0; i < num; i++ {
|
||||
|
@@ -11,6 +11,7 @@ URL: <input type="text" name="url">
|
||||
<input type="submit" value="Add">
|
||||
</form>
|
||||
`
|
||||
|
||||
var store = NewURLStore()
|
||||
|
||||
func main() {
|
||||
@@ -29,7 +30,6 @@ func Redirect(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, url, http.StatusFound)
|
||||
}
|
||||
|
||||
|
||||
func Add(w http.ResponseWriter, r *http.Request) {
|
||||
url := r.FormValue("url")
|
||||
if url == "" {
|
||||
|
@@ -23,7 +23,6 @@ func Redirect(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, url, http.StatusFound)
|
||||
}
|
||||
|
||||
|
||||
func Add(w http.ResponseWriter, r *http.Request) {
|
||||
url := r.FormValue("url")
|
||||
if url == "" {
|
||||
|
@@ -5,8 +5,8 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"net/rpc"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
@@ -4,4 +4,3 @@ package main
|
||||
func main() {
|
||||
println("Hello", "world")
|
||||
}
|
||||
|
||||
|
@@ -8,5 +8,6 @@ import (
|
||||
func main() {
|
||||
fmt.Printf("%s", runtime.Version())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// go1.0.3 or go 1.1
|
||||
|
@@ -24,6 +24,7 @@ const signTemplateHTML = `
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
var signTemplate = template.Must(template.New("sign").Parse(signTemplateHTML))
|
||||
|
||||
func init() {
|
||||
@@ -43,4 +44,3 @@ func sign(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, err.String(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -28,6 +28,7 @@ const guestbookTemplateHTML = `
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
var guestbookTemplate = template.Must(template.New("book").Parse(guestbookTemplateHTML))
|
||||
|
||||
type Greeting struct {
|
||||
|
@@ -13,4 +13,3 @@ func init() {
|
||||
func handle(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, "<html><body>Hello, World! 세상아 안녕!! </body></html>")
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,5 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
@@ -12,6 +13,7 @@ func main() {
|
||||
fmt.Printf("32 bit int is: %d\n", m)
|
||||
fmt.Printf("16 bit int is: %d\n", n)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
32 bit int is: 34
|
||||
16 bit int is: 34
|
||||
|
@@ -14,6 +14,7 @@ func main() {
|
||||
fmt.Printf("%X - %X - %X\n", ch, ch2, ch3)
|
||||
fmt.Printf("%U - %U - %U", ch, ch2, ch3)
|
||||
}
|
||||
|
||||
/* Ouput:
|
||||
65 - 946 - 1053236
|
||||
A - β -
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package main
|
||||
|
||||
var a string
|
||||
|
||||
func main() {
|
||||
a = "G"
|
||||
print(a)
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package main
|
||||
|
||||
var a = "G"
|
||||
|
||||
func main() {
|
||||
n()
|
||||
m()
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package main
|
||||
|
||||
var a = "G"
|
||||
|
||||
func main() {
|
||||
n()
|
||||
m()
|
||||
|
@@ -11,4 +11,5 @@ func main() {
|
||||
fmt.Printf("T/F? Does the string \"%s\" have prefix %s? ", str, "Th")
|
||||
fmt.Printf("%t\n", strings.HasPrefix(str, "Th"))
|
||||
}
|
||||
|
||||
// Output: T/F? Does the string "This is an example of a string" have prefix Th? true
|
@@ -22,6 +22,7 @@ func main() {
|
||||
fmt.Printf("%2.2f / ", 100*rand.Float32())
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
134020434 / 1597969999 / 1721070109 / 2068675587 / 1237770961 / 220031192 / 2031484958 / 583324308 / 958990240 / 413002649 / 6 / 7 / 2 / 1 / 0 /
|
||||
22.84 / 10.12 / 44.32 / 58.58 / 15.49 / 12.23 / 30.16 / 88.48 / 34.26 / 27.18 /
|
||||
|
@@ -24,6 +24,7 @@ func main() {
|
||||
str3 := strings.Join(sl2, ";")
|
||||
fmt.Printf("sl2 joined by ;: %s\n", str3)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Splitted in slice: [The quick brown fox jumps over the lazy dog]
|
||||
The - quick - brown - fox - jumps - over - the - lazy - dog -
|
||||
|
@@ -7,5 +7,6 @@ func main() {
|
||||
*p = 0
|
||||
|
||||
}
|
||||
|
||||
// in Windows: stops only with: <exit code="-1073741819" msg="process crashed"/>
|
||||
// runtime error: invalid memory address or nil pointer dereference
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./trans"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var twoPi = 2 * trans.Pi // decl computes twoPi
|
||||
|
@@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./trans"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var twoPi = 2 * trans.Pi
|
||||
|
||||
func main() {
|
||||
fmt.Printf("2*Pi = %g\n", twoPi) // 2*Pi = 6.283185307179586
|
||||
}
|
@@ -15,6 +15,7 @@ func main() {
|
||||
fmt.Printf("Character on position %d is: %c \n", ix, str2[ix])
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The length of str is: 27
|
||||
Character on position 0 is: G
|
||||
|
@@ -20,6 +20,7 @@ func main() {
|
||||
fmt.Printf("%-2d %d %U '%c' % X\n", index, rune, rune, rune, []byte(string(rune)))
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The length of str is: 27
|
||||
Character on position 0 is: G
|
||||
|
@@ -25,6 +25,7 @@ func doDBOperations() {
|
||||
return //terminate the program
|
||||
// deferred function executed here just before actually returning, even if there is a return or abnormal termination before
|
||||
}
|
||||
|
||||
/* Output:
|
||||
ok, connected to db
|
||||
Defering the database disconnect.
|
||||
@@ -33,6 +34,3 @@ Oops! some crash or network error ...
|
||||
Returning from function here!
|
||||
ok, disconnected from db
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
@@ -16,4 +16,5 @@ func func1(s string) (n int, err error) {
|
||||
func main() {
|
||||
func1("Go")
|
||||
}
|
||||
|
||||
// Output: 2011/10/04 10:46:11 func1("Go") = 7, EOF
|
@@ -25,6 +25,7 @@ func fibonacci(n int) (res int) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/* Output:
|
||||
fibonacci(0) is: 1
|
||||
fibonacci(1) is: 1
|
||||
|
@@ -7,6 +7,7 @@ import (
|
||||
)
|
||||
|
||||
const LIM = 41
|
||||
|
||||
var fibs [LIM]uint64
|
||||
|
||||
func main() {
|
||||
@@ -35,6 +36,7 @@ func fibonacci(n int) (res uint64) {
|
||||
fibs[n] = res
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
Output: LIM=40:
|
||||
normal (fibonacci.go): the calculation took this amount of time: 4.730270 s
|
||||
|
@@ -1,5 +1,6 @@
|
||||
// filter_factory.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type flt func(int) bool
|
||||
@@ -44,6 +45,7 @@ func main(){
|
||||
fmt.Println("Bigger than 4: ", bigger)
|
||||
fmt.Println("Smaller than or equal to 4: ", smaller)
|
||||
}
|
||||
|
||||
/*
|
||||
s = [1 2 3 4 5 7]
|
||||
odd = [1 3 5 7]
|
||||
|
@@ -1,5 +1,6 @@
|
||||
// function_filter.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type flt func(int) bool
|
||||
@@ -41,6 +42,7 @@ func main(){
|
||||
even := filter(slice, isEven)
|
||||
fmt.Println("Even elements of slice are: ", even)
|
||||
}
|
||||
|
||||
/*
|
||||
slice = [1 2 3 4 5 7]
|
||||
Odd elements of slice are: [1 3 5 7]
|
||||
|
@@ -1,4 +1,5 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
@@ -13,8 +13,8 @@ func Add(a,b int) {
|
||||
fmt.Printf("The sum of %d and %d is: %d\n", a, b, a+b)
|
||||
}
|
||||
|
||||
|
||||
func callback(y int, f func(int, int)) {
|
||||
f(y, 2) // this becomes Add(1, 2)
|
||||
}
|
||||
|
||||
// Output: The sum of 1 and 2 is: 3
|
||||
|
@@ -1,4 +1,5 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
@@ -10,17 +11,18 @@ func main() {
|
||||
fmt.Printf("The result is: %v\n", TwoAdder(2))
|
||||
}
|
||||
|
||||
func Add2() (func(b int) int) {
|
||||
func Add2() func(b int) int {
|
||||
return func(b int) int {
|
||||
return b + 2
|
||||
}
|
||||
}
|
||||
|
||||
func Adder(a int) (func(b int) int) {
|
||||
func Adder(a int) func(b int) int {
|
||||
return func(b int) int {
|
||||
return a + b
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Call Add2 for 2 gives: 4
|
||||
The result is: 4
|
||||
|
@@ -12,16 +12,22 @@ func main() {
|
||||
}
|
||||
|
||||
func even(nr int) bool {
|
||||
if nr == 0 {return true}
|
||||
if nr == 0 {
|
||||
return true
|
||||
}
|
||||
return odd(RevSign(nr) - 1)
|
||||
}
|
||||
|
||||
func odd(nr int) bool {
|
||||
if nr == 0 {return false}
|
||||
if nr == 0 {
|
||||
return false
|
||||
}
|
||||
return even(RevSign(nr) - 1)
|
||||
}
|
||||
|
||||
func RevSign(nr int) int {
|
||||
if nr < 0 {return -nr}
|
||||
if nr < 0 {
|
||||
return -nr
|
||||
}
|
||||
return nr
|
||||
}
|
@@ -15,4 +15,5 @@ func f() (ret int) {
|
||||
func main() {
|
||||
fmt.Println(f())
|
||||
}
|
||||
|
||||
// Output: 2
|
||||
|
@@ -22,6 +22,7 @@ func Min(a ...int) int {
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
/*
|
||||
The minimum is: 0
|
||||
The minimum in the array arr is: 1
|
||||
|
@@ -14,4 +14,5 @@ func Sum(a *[3]float64) (sum float64) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Output: The sum of the array is: 24.600000
|
||||
|
@@ -8,4 +8,5 @@ func main() {
|
||||
fmt.Printf("%d:%c ", i, c)
|
||||
}
|
||||
}
|
||||
|
||||
// prints: 0:ÿ 2:界
|
@@ -11,6 +11,7 @@ const (
|
||||
)
|
||||
|
||||
type pixel int
|
||||
|
||||
var screen [WIDTH][HEIGHT]pixel
|
||||
|
||||
func main() {
|
||||
@@ -29,6 +30,7 @@ func main() {
|
||||
|
||||
fmt.Println(screen)
|
||||
}
|
||||
|
||||
/* Output for WIDTH = 5 and HEIGHT = 4:
|
||||
[[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]
|
||||
[[1 1 1 1] [1 1 1 1] [1 1 1 1] [1 1 1 1] [1 1 1 1]]
|
||||
|
@@ -14,6 +14,7 @@ func main() {
|
||||
fmt.Printf("Slice at %d is: %d\n", ix, value)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Slice at 0 is: 1
|
||||
Slice at 1 is: 2
|
||||
|
@@ -18,6 +18,7 @@ func main() {
|
||||
fmt.Printf("%d ", ix)
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Season 0 is: Spring
|
||||
Season 1 is: Summer
|
||||
|
@@ -20,10 +20,10 @@ func main() {
|
||||
fmt.Printf("Key: %v, Value: %v / ", k, v)
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
inverted:
|
||||
Key: 12, Value: foxtrot / Key: 16, Value: hotel / Key: 87, Value: delta / Key: 23, Value: charlie /
|
||||
Key: 65, Value: juliet / Key: 43, Value: kilo / Key: 56, Value: bravo / Key: 98, Value: lima /
|
||||
Key: 34, Value: golf /
|
||||
*/
|
||||
|
||||
|
@@ -20,6 +20,7 @@ func main() {
|
||||
fmt.Printf("Map assigned at \"two\" is: %d\n", mapLit["two"])
|
||||
fmt.Printf("Map literal at \"ten\" is: %d\n", mapLit["ten"])
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Map literal at "one" is: 1
|
||||
Map created at "key2" is: 3.141590
|
||||
|
@@ -1,5 +1,6 @@
|
||||
// map_func.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
@@ -10,4 +11,5 @@ func main() {
|
||||
}
|
||||
fmt.Println(mf)
|
||||
}
|
||||
|
||||
// Output: map[1:0x10903be0 5:0x10903ba0 2:0x10903bc0]
|
||||
|
@@ -21,6 +21,7 @@ func main() {
|
||||
}
|
||||
fmt.Printf("Version B: Value of items: %v\n", items2)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Version A: Value of items: [map[1:2] map[1:2] map[1:2] map[1:2] map[1:2]]
|
||||
Version B: Value of items: [map[] map[] map[] map[] map[]]
|
||||
|
@@ -19,7 +19,7 @@ func main() {
|
||||
}
|
||||
keys := make([]string, len(barVal))
|
||||
i := 0
|
||||
for k, _ := range barVal {
|
||||
for k := range barVal {
|
||||
keys[i] = k
|
||||
i++
|
||||
}
|
||||
@@ -30,6 +30,7 @@ func main() {
|
||||
fmt.Printf("Key: %v, Value: %v / ", k, barVal[k])
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
unsorted:
|
||||
Key: indio, Value: 87 / Key: echo, Value: 56 / Key: juliet, Value: 65 / Key: charlie, Value: 23 /
|
||||
@@ -40,4 +41,3 @@ Key: alpha, Value: 34 / Key: bravo, Value: 56 / Key: charlie, Value: 23 / Key: d
|
||||
Key: echo, Value: 56 / Key: foxtrot, Value: 12 / Key: golf, Value: 34 / Key: hotel, Value: 16 /
|
||||
Key: indio, Value: 87 / Key: juliet, Value: 65 / Key: kilo, Value: 43 / Key: lima, Value: 98 /
|
||||
*/
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user