我正在通过来刷新我的记忆,并且无意中发现了类似的二叉树练习。我已经编写了一些代码来遍历看起来应该工作的二叉树。
package main
import (
"golang.org/x/tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
if t == nil {
return
}
c
我正在做Go之旅中的练习,但我遇到了一个我不能解决的问题。
我正在做,我得到了这个错误:
256 x 256
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
main.Pic(0x100, 0x100)
/tmp/sandbox1628012103/prog.go:14 +0xcf
golang.org/x/tour/pic.Show(0xc0000001a0)
/tmp/gopath962180923/pkg/mod/golang.org/x/tour@v0
我在做“围棋之旅”的练习,我在上面的页面是
下面是我的代码:
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var ret [][]uint8;
var row []uint8;
for i:=uint8(0);i<uint8(dy);i++ {
row = []uint8 {}
for j:=uint8(0);j<uint8(dx);j++ {
row = append(r
我正在尝试解决Go Tour的练习。
我的解决方案失败,并显示错误消息the process to take too long
出什么问题了?
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
guess := 1.0
i := 1
for i < 10 {
guess = guess - (math.Pow(guess, 2)-x)/(2*guess)
}
return guess
}
fun
我正在尝试解决go tour上的练习。这就是我所做的;
package main
import "tour/tree"
import "fmt"
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
if t.Left != nil {
Walk(t.Left, ch)
}
ch <- t.Value
if t.Right
当我到达第二页时,我很难在多页的巡回赛中启动Bootstrap旅游。
我的巡演从点击事件开始,localStorage设置为false。这个巡演从点击事件开始很好,但是当我进入第二步并加载一个新的页面时,这个浏览就不会从它停止的地方开始。
在新的一页上,我该如何继续从第二步开始呢?我知道我需要重新初始化巡演,但我显然做得不对。
$(document).ready(function () {
// Instance the tour
var tour = new Tour({
name: "CiteTour",
steps: [{
我试图在这个tour之后第一次运行go 程序。
我已经运行了下一个命令:
go install golang.org/x/website/tour@latest
但是,当我试图运行go run tour时,我得到了下一个例外:
package tour is not in GOROOT (/usr/local/go/src/tour)
我还没有找到任何关于如何运行tour程序的额外文档。
如何正确运行tour程序?也许我做错了什么?
我正在尝试运行命令go get golang.org/x/tour tour,但是我得到了错误消息"package tour: unrecognized import path "tour" (import path does not begin with hostname)"。我也没有得到我提到的bin文件夹中的二进制文件。
我试图解决这个问题的一些事情:
阅读和上的文档。
验证工作空间文件夹是否如文档中指定的那样存在(我的工作区是/home/USERNAME/go,在其中我已经创建了2个文件夹src和bin)。
将命令从go get golang
我正在学习频道如何在围棋中工作,并偶然发现了关闭频道的问题。这是Go之旅的一个修改示例,它生成n-1斐波纳契数并通过通道发送,留下通道容量的最后一个“元素”未使用。
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n-1; i++ {
c <- x
x, y = y, x+y
}
// close(c) // It's commented out on purpose
}
func main() {
n := 10
我正在尝试完成Go教程https://tour.golang.org/methods/18中的这个练习,为一个由四个字节组成的数组组成的IPAddr类型实现一个String()方法。到目前为止,我已经尝试过: package main
import (
"fmt"
"strings"
)
type IPAddr [4]byte
func (ipaddr IPAddr) String() string {
ipaddrStrings := make([]string, 4)
for i, b := range ipaddr
我正在学习Go,并在GoTours上从事的工作。这是我到目前为止所掌握的。
package main
import (
"fmt"
"code.google.com/p/go-tour/tree"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
if t != nil {
Walk(t.Left, ch)
ch
我正在阅读Go的官方教程,很难理解频道和缓冲频道之间的区别。到教程的链接是和。
在频道教程中,c频道首先收到7,2,8之和为17,然后收到-9,4,0之和为-5。当从c中读取时,它首先以LIFO顺序输出5到x,然后从17输出到y:
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func main() {
我们能在去朗开两次吗?
package main
import (
"fmt"
)
func emit(c chan string) {
words := []string {"The", "quick", "brown", "fox"}
for _, word := range words {
c <- word
}
close(c)
}
在函数main中,如果我尝试两次使用同一个通道,我将得到该通道的默认值
func main() {
我完成了进行树比较的go巡回练习(#69),并且能够有效地比较两棵树。
是代码
package main
import (
"fmt"
"golang.org/x/tour/tree"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
if t == nil {
return
}
Walk(t.Left, ch)
我通过在官方网站上浏览Go的Tour of Go开始尝试Go。
我只有基本的编程经验,但在进入频道页面时,我开始尝试去理解它,但我最终还是很困惑。
下面是我的代码:
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
s[0] = 8
s = append(s, 20)
fmt.Println(s)
for _, v := range s {
sum += v
}
c <- sum // send sum to c
go version go1.12.9 darwin/amd64
在我安装了golang.org/x/tour包之后,我运行了命令tour,得到一个错误:
Couldn't find tour files: could not find go-tour content; check $GOROOT and $GOPATH
下面是go env的输出
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/elk/Library/Caches/go-build"
GOEXE=""
GOFLAGS=
如果我有多个阅读和写入同一频道,有什么办法关闭通道在特定的点,例如,在一定数量的元素被写入。
func foo(c chan int) {
for i := range c {
// how to close the channel after 100 integers are written?
c <- i + 1
c <- i + 2
}
}
func bar() {
c := make(chan int, 200)
c <- 0
go foo(c)
go foo(c)
我无法关闭频道时,不知道它的
长度
package main
import (
"fmt"
"time"
)
func gen(ch chan int) {
var i int
for {
time.Sleep(time.Millisecond * 10)
ch <- i
i++
// when no more data (e.g. from db, or event stream)
if i > 100 {
b
我试着在Go中学习频道,所以这是一个人为的例子,一个频道有多个作者,但只有一个读者。这是一个非常基本的例子,但我想更进一步,设想它是一个http服务器,其中为每个新请求创建了一个goroutine,并且每个goroutine在一个通道上写了一个。
func Start2(){
var wg sync.WaitGroup
wg.Add(2)
c := make(chan int)
go func(){
defer wg.Done()
i := 0
for {
i+=2