forked from unknwon/the-way-to-go_ZH_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
447 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package main | ||
import fm "fmt" // alias3 | ||
|
||
func main() { | ||
fm.Println("hello, world") | ||
import fm "fmt" // alias | ||
|
||
func main() { | ||
fm.Println("hello, world") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
import "fmt" | ||
|
||
func main() { | ||
var n int16 = 34 | ||
var m int32 | ||
|
||
// compiler error: cannot use n (type int16) as type int32 in assignment | ||
//m = n | ||
m = int32(n) | ||
|
||
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// char.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
var ch int = '\u0041' | ||
var ch2 int = '\u03B2' | ||
var ch3 int = '\U00101234' | ||
fmt.Printf("%d - %d - %d\n", ch, ch2, ch3) | ||
fmt.Printf("%c - %c - %c\n", ch, ch2, ch3) | ||
fmt.Printf("%X - %X - %X\n", ch, ch2, ch3) | ||
fmt.Printf("%U - %U - %U", ch, ch2, ch3) | ||
} | ||
/* Ouput: | ||
65 - 946 - 1053236 | ||
A - β - | ||
41 - 3B2 - 101234 | ||
U+0041 - U+03B2 - U+101234 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
var str string = "Hello, how is it going, Hugo?" | ||
var manyG = "gggggggggg" | ||
|
||
fmt.Printf("Number of H's in %s is: ", str) | ||
fmt.Printf("%d\n", strings.Count(str, "H")) | ||
|
||
fmt.Printf("Number of double g's in %s is: ", manyG) | ||
fmt.Printf("%d\n", strings.Count(manyG, "gg")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
var goos string = os.Getenv("GOOS") | ||
fmt.Printf("The operating system is: %s\n", goos) | ||
path := os.Getenv("PATH") | ||
fmt.Printf("Path is %s\n", path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
// hello_world.go | ||
package main | ||
|
||
import "fmt" | ||
import "fmt" | ||
|
||
func main() { | ||
func main() { | ||
fmt.Println("hello, world") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
var str string = "Hi, I'm Marc, Hi." | ||
|
||
fmt.Printf("The position of \"Marc\" is: ") | ||
fmt.Printf("%d\n", strings.Index(str, "Marc")) | ||
|
||
fmt.Printf("The position of the first instance of \"Hi\" is: ") | ||
fmt.Printf("%d\n", strings.Index(str, "Hi")) | ||
fmt.Printf("The position of the last instance of \"Hi\" is: ") | ||
fmt.Printf("%d\n", strings.LastIndex(str, "Hi")) | ||
|
||
fmt.Printf("The position of \"Burger\" is: ") | ||
fmt.Printf("%d\n", strings.Index(str, "Burger")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package trans | ||
|
||
import "math" | ||
|
||
var Pi float64 | ||
|
||
func init() { | ||
Pi = 4 * math.Atan(1) // init() function computes Pi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var i1 = 5 | ||
fmt.Printf("An integer: %d, its location in memory: %p\n", i1, &i1) | ||
|
||
var intP *int | ||
intP = &i1 | ||
fmt.Printf("The value at memory location %p is %d\n", intP, *intP) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
var str string = "This is an example of a string" | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
func main() { | ||
for i := 0; i < 10; i++ { | ||
a := rand.Int() | ||
fmt.Printf("%d / ", a) | ||
} | ||
for i := 0; i < 5; i++ { | ||
r := rand.Intn(8) | ||
fmt.Printf("%d / ", r) | ||
} | ||
fmt.Println() | ||
timens := int64(time.Now().Nanosecond()) | ||
rand.Seed(timens) | ||
for i := 0; i < 10; i++ { | ||
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 / | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
var origS string = "Hi there! " | ||
var newS string | ||
|
||
newS = strings.Repeat(origS, 3) | ||
fmt.Printf("The new repeated string is: %s\n", newS) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
func main() { | ||
var orig string = "666" | ||
var an int | ||
var newS string | ||
|
||
fmt.Printf("The size of ints is: %d\n", strconv.IntSize) | ||
|
||
an, _ = strconv.Atoi(orig) | ||
fmt.Printf("The integer is: %d\n", an) | ||
an = an + 5 | ||
newS = strconv.Itoa(an) | ||
fmt.Printf("The new string is: %s\n", newS) | ||
} |
Oops, something went wrong.