加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
hellogolang.go.default 4.07 KB
一键复制 编辑 原始数据 按行查看 历史
993 提交于 2018-02-28 17:43 . 做到方法章节的,错误练习
/*
package main
import "fmt"
func main() {
// 指向 int 类型的 指针类型.
var p *int
i, j := 42, 2701
p = &i
fmt.Println(*p)
*p = 21
fmt.Println(i)
p = &j
*p = *p / 37
fmt.Println(j)
}
*/
/*
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
//fmt.Println(Vertex{1, 2})
v := Vertex{1, 2}
fmt.Println(v.X)
v.X = 4
fmt.Println(v)
}
*/
/*
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
p := &v
p.X = 1e9
fmt.Println(v)
}
*/
/*
package main
import "fmt"
type Vertex struct {
X, Y int
}
var (
v1 = Vertex{1, 2}
v2 = Vertex{X: 1}
v3 = Vertex{}
p = &Vertex{1, 2}
)
func main() {
fmt.Println(v1, p, v2, v3)
}
*/
/*
package main
import "fmt"
func main() {
var a [2]string
a[0] = "Hello"
a[1] = "world"
fmt.Println(a)
}
*/
/*
package main
import "fmt"
func main() {
p := []int{2, 3, 5, 7, 11, 13}
fmt.Println("p ==", p)
for i := 0; i < len(p); i++ {
fmt.Printf("p[%d] == %d\n", i, p[i])
}
}
*/
/*
package main
import "fmt"
func main() {
p := []int{2, 3, 5, 7, 11, 13}
fmt.Println("p ==", p)
fmt.Println("p[1:4] =", p[1:4])
fmt.Println("p[:3] =", p[:3])
fmt.Println("p[4:] =", p[4:])
}
*/
/*
package main
import "fmt"
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
func main() {
a := make([]int, 5)
printSlice("a", a)
b := make([]int, 0, 5)
printSlice("b", b)
c := b[:2]
printSlice("c", c)
d := c[2:5]
printSlice("d", d)
}
*/
/*
package main
import "fmt"
func main() {
var z []int
fmt.Println(z, len(z), cap(z))
if z == nil {
fmt.Println("nil!")
}
}
*/
/*
package main
import "fmt"
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
func main() {
var a []int
printSlice("a", a)
a = append(a, 0)
printSlice("a", a)
a = append(a, 1)
printSlice("a", a)
a = append(a, 2, 3, 4)
printSlice("a", a)
}
*/
/*
package main
import "fmt"
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
func main() {
for i, v := range pow {
fmt.Printf("2**%d = %d\n", i, v)
}
}
*/
/*
package main
import "fmt"
func main() {
pow := make([]int, 10)
for i := range pow {
pow[i] = 1 << uint(i)
}
fmt.Println(pow)
for _, value := range pow {
fmt.Printf("%d\n", value)
}
}
*/
/*
package main
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m map[string]Vertex
func main() {
fmt.Println(m)
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, -74.39967,
}
fmt.Println(m["Bell Labs"])
}
*/
/*
package main
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m = map[string]Vertex{
"Bell Labs": Vertex{
40.68433, -74.39967,
},
"Google": Vertex{
37.42202, -122.08408,
},
}
func main() {
fmt.Println(m)
}
*/
/*
package main
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
"Google": {37.42202, -122.08408},
}
func main() {
fmt.Println(m)
}
*/
/*
package main
import "fmt"
func main() {
m := make(map[string]int)
m["Answer"] = 42
fmt.Println("The value:", m["Answer"])
m["Answer"] = 48
fmt.Println("The value:", m["Answer"])
delete(m, "Answer")
fmt.Println("The value:", m["Answer"])
v, ok := m["Answer"]
fmt.Println("The value:", v, "Present?", ok)
}
*/
/*
package main
import (
"fmt"
"math"
)
func main() {
hypot := func(x, y float64) float64 {
return math.Sqrt(x*x + y*y)
}
fmt.Println(hypot(3, 4))
}
*/
/*
package main
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, neg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}
}
*/
package main
import "fmt"
// 斐波那契数列实现
func fibonacci() func() []int {
var result []int
return func() []int {
size := len(result)
if size <= 1 {
result = append(result, 1)
} else {
result = append(result, result[size-2]+result[size-1])
}
return result
}
}
func main() {
f := fibonacci()
for i := 0; i < 20; i++ {
fmt.Println(f())
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化