小编fai*_*_kk的帖子

返回频道Golang

我正在尝试使用Go频道并与go blog下面的功能示例混淆:

func gen(nums []int) <-chan int {
    out := make(chan int)
    go func() {
        for _, n := range nums {
            out <- n
        }
        close(out)
    }()
    fmt.Println("return statement is called ")
    return out
}
Run Code Online (Sandbox Code Playgroud)

主要:

func main() {
    c := make(chan int)
    c = gen([]int{2, 3, 4, 5})

    // Consume the output.
    // Print 2,3,4,5
    fmt.Println(<-c)
    fmt.Println(<-c)
    fmt.Println(<-c)
    fmt.Println(<-c)
}
Run Code Online (Sandbox Code Playgroud)

完整代码:http://play.golang.org/p/Qh30wzo4m0

我怀疑:

  1. 我的理解是,一旦return被调用,函数将被终止,并且该函数内的通道不再有生命.

  2. return声明只被调用一次.但是out频道的内容被多次阅读.在这种情况下,实际的执行流程是什么?

(我是并发编程的新手.)

go

11
推荐指数
1
解决办法
1万
查看次数

用于 dynamodb 的 Golang 包,支持映射、列表和 JSON

我正在尝试在 dynamodb 中保存 JSON 对象,使用新添加的对 JSON 类型的支持(我的理解是 JSON 类型基本上是映射+列表),以便我可以查询和修改嵌套的 JSON 文档。

我找不到任何带有新添加的数据类型支持的 dynamodb golang 包。

请问对此有何建议?

go amazon-dynamodb

6
推荐指数
1
解决办法
9900
查看次数

xml.NewDecoder(resp.Body).Decode 出现 EOF 错误 _GOLang

我正在尝试从 html 响应中解码 XML。

=>我将此响应正文作为字符串保存到变量中,并使用 xml.Unmarshal 函数 .Code 成功解码:

    err = xml.Unmarshal([]byte(outs), &v)
       if err != nil {
    fmt.Printf("error is here: %v", err)
    return
                       }
Run Code Online (Sandbox Code Playgroud)

所以我认为问题不在于响应正文的实际内容。

现在我的实际代码:

req1, err := http.NewRequest("GET", concat([]string{domain, defects_link}), nil)
error_handler(err)
req1.Close = true //I tried with and without this line 

resp1, err := client.Do(req1)
error_handler(err)

fmt.Printf("\n %s \n", resp1.Status)

defer resp1.Body.Close()//I tried with and without this line
conts1, err := ioutil.ReadAll(resp1.Body)
error_handler(err)
fmt.Println("Response Body is Here :", string(conts1))//Contents are Printed Here
Run Code Online (Sandbox Code Playgroud)

响应打印在上面代码的最后一行。但下面的代码给出“错误:EOF”

    if err := …
Run Code Online (Sandbox Code Playgroud)

html xml go

4
推荐指数
1
解决办法
2625
查看次数

插入缺失值不工作GoLang

我正在尝试插入一个Int值来切片,如果它缺少那么.

我的代码:

package main

import (
    "fmt"
)

func AppendIfMissing(slice []int, i int) []int {
    for _, ele := range slice {
        if ele == i {
            fmt.Println(i)
            return slice
        }
    }
    fmt.Println("i value is ", i)
    slice = append(slice, i)
    return slice
}

func main() {
    slice1 := []int{1, 2, 3, 4}
    AppendIfMissing(slice1, 60)
    fmt.Println("slice after adding :", slice1)
}
Run Code Online (Sandbox Code Playgroud)

OutPut:

    i value is  60
    slice after adding : [1 2 3 4]
Run Code Online (Sandbox Code Playgroud)

没有附加到切片.我的代码出了什么问题?

go slice

3
推荐指数
1
解决办法
165
查看次数

向外扩展切片和底层数组

我有一个数组和一个指向它的切片,如下所示:

package main

import "fmt"

func main() {

    array_str := []string{"0a","1b","2c","3d","4e"}
    slice_str:=array_str[1:4]
    fmt.Println("Initially :")
    fmt.Println("Printing 1 :Array :",array_str)
    fmt.Println("Printing 1 :Slice:",slice_str)

    //Step 1.Changing Slice and it get reflected in array
    fmt.Println("\nAfter Alteration:")
    slice_str[0]="alterd_1b"
    fmt.Println("Printing 2 :Array :",array_str)
    fmt.Println("Printing 2 :Slice:",slice_str)
    fmt.Println("len of  slice_str:",len(slice_str)," cap of         slice_str:",cap(slice_str),"len of  array_str:",len(array_str))

    //Step 2.appending to slice and it get reflected

    slice_str = append(slice_str,"apnded_elemnt")
    fmt.Println("\nAfter Apending:")
    fmt.Println("Printing 3 :Array :",array_str)//"4e" is replaced with "apnded_elemnt" in array !!
    fmt.Println("Printing 3 :Slice:",slice_str)
    fmt.Println("len of  slice_str:",len(slice_str)," cap of …
Run Code Online (Sandbox Code Playgroud)

go slice

1
推荐指数
1
解决办法
518
查看次数

标签 统计

go ×5

slice ×2

amazon-dynamodb ×1

html ×1

xml ×1