最赞回答 / 慕工程3002471
func (person Person) Grow() { person.Age++ }
p := Person{"Robert", "Male", 33, "Beijing"} p.Grow() fmt.Printf("%v\n", p)如果执行p.Grow()不能达到预期目的的话,要这个·值方法·功能有何用处呢?至少在这个例子里是没用的啊
2018-07-11
最简单没有之一
func main() {
for i := 0; i < 10; i++ {
fmt.Printf("%d ", fibonacci(i))
defer fmt.Printf("%d ", fibonacci(i))
}
}
func fibonacci(num int) int {
if num == 0 {
return 0
}
if num < 2 {
return 1
}
return fibonacci(num-1) + fibonacci(num-2)
}
func main() {
for i := 0; i < 10; i++ {
fmt.Printf("%d ", fibonacci(i))
defer fmt.Printf("%d ", fibonacci(i))
}
}
func fibonacci(num int) int {
if num == 0 {
return 0
}
if num < 2 {
return 1
}
return fibonacci(num-1) + fibonacci(num-2)
}
2018-07-11
package main
import "fmt"
func main() {
var number int = 5
if number += 4; 10 > number {
number += 27
number += 3
fmt.Print(number)
} else if 10 < number {
number -= 2
fmt.Print(number)
}
fmt.Println(number)
}
import "fmt"
func main() {
var number int = 5
if number += 4; 10 > number {
number += 27
number += 3
fmt.Print(number)
} else if 10 < number {
number -= 2
fmt.Print(number)
}
fmt.Println(number)
}
2018-07-10
package main
import "fmt"
type Animal interface {
}
type Cat struct {
name string
age int
addr string
}
func main() {
myCat := Cat{"Little C", 2, "In the house"}
animal, ok := interface{}(&myCat).(Animal)
fmt.Print(animal)
fmt.Printf("%v, %v\n", ok, animal)
}
import "fmt"
type Animal interface {
}
type Cat struct {
name string
age int
addr string
}
func main() {
myCat := Cat{"Little C", 2, "In the house"}
animal, ok := interface{}(&myCat).(Animal)
fmt.Print(animal)
fmt.Printf("%v, %v\n", ok, animal)
}
2018-07-05
最新回答 / 慕仔0371871
定义main函数的源代码文件中必须声明package main,但是main并不是所在的文件夹(代码包)的名称。go build main 把main当成了一个代码包(文件夹)的名称,如果你没有在GOROOT/src或GOPATH/src下 显式的创建过一个名为main的代码包,就会报这个错误。
2018-07-02
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
go func() {
fmt.Println("1")
ch1 <- 1
}()
go func() {
<- ch1
fmt.Println("2")
ch2 <- 1
}()
go func() {
<- ch2
fmt.Println("3")
ch3 <- 1
}()
<- ch3
}
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
go func() {
fmt.Println("1")
ch1 <- 1
}()
go func() {
<- ch1
fmt.Println("2")
ch2 <- 1
}()
go func() {
<- ch2
fmt.Println("3")
ch3 <- 1
}()
<- ch3
}
2018-06-05
答案:
func main() {
var so sync.WaitGroup
so.Add(1)
go func() {
fmt.Println("1")
so.Done()
}()
so.Wait()
so.Add(1)
go func() {
fmt.Println("2")
so.Done()
}()
so.Wait()
so.Add(1)
go func() {
fmt.Println("3")
so.Done()
}()
so.Wait()
}
func main() {
var so sync.WaitGroup
so.Add(1)
go func() {
fmt.Println("1")
so.Done()
}()
so.Wait()
so.Add(1)
go func() {
fmt.Println("2")
so.Done()
}()
so.Wait()
so.Add(1)
go func() {
fmt.Println("3")
so.Done()
}()
so.Wait()
}
2018-06-03
答案:
select {
case e, ok := <-ch4:
if !ok {
fmt.Println("End.")
return
}
fmt.Println(e)
close(ch4)
default:
fmt.Println("No Data!")
ch4 <- 1
}
select {
case e, ok := <-ch4:
if !ok {
fmt.Println("End.")
return
}
fmt.Println(e)
close(ch4)
default:
fmt.Println("No Data!")
ch4 <- 1
}
2018-06-03
答案:
ia := []interface{}{byte(6), 'a', uint(10), int32(-4)}
switch v := ia[rand.Intn(4)]; interface{}(v).(type) {
case int32 :
fmt.Printf("Case A.")
case byte :
fmt.Printf("Case B.")
default:
fmt.Println("Unknown!")
}
ia := []interface{}{byte(6), 'a', uint(10), int32(-4)}
switch v := ia[rand.Intn(4)]; interface{}(v).(type) {
case int32 :
fmt.Printf("Case A.")
case byte :
fmt.Printf("Case B.")
default:
fmt.Println("Unknown!")
}
2018-06-03
最赞回答 / sliverg
这就是一个slice, interface的意思是这个slice里面可以是任何类型, byte(6),uint(10) ,这些都是类型转换,都是一个实数。还有你这个有错误,'a'这个会编译报错,GO不支持单引号实际上你可以理解为这样一个slice []interface{1,"a",3,4,5} ,
2018-06-03