小编icz*_*cza的帖子

在go中查找constant的地址

我们编写了一个程序,通过它我们试图找到一个常量的地址.有可能这样做吗?

package main

func main() {
        const k = 5
        address := &k
}
Run Code Online (Sandbox Code Playgroud)

它给出了一个错误,任何人都可以告诉我们如何找到常量的地址?

pointers constants go

13
推荐指数
2
解决办法
8069
查看次数

用相同的变量替换Sprintf中的所有变量

是否可以使用fmt.Sprintf()相同的值替换格式化字符串中的所有变量?

就像是:

val := "foo"
s := fmt.Sprintf("%v in %v is %v", val)
Run Code Online (Sandbox Code Playgroud)

哪会回来

"foo in foo is foo"
Run Code Online (Sandbox Code Playgroud)

string format printf go

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

为什么Go允许编译未使用的函数参数?

Go来自C的一个比较值得注意的方面是,如果在其中声明了未使用的变量,编译器将不会构建您的程序.那么,如果在函数中声明了未使用的参数,那么为什么要构建这个程序呢?

func main() {
    print(computron(3, -3));
}


func computron(param_a int, param_b int) int {
    return 3 * param_a;
}
Run Code Online (Sandbox Code Playgroud)

variables parameters compilation go

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

在Golang中访问Type map [string] interface {}的嵌套Map

所以我正在尝试解析JSON响应.它可以是多层次的.这就是我做的:

var result map[string]interface{}
json.Unmarshal(apiResponse, &result)
Run Code Online (Sandbox Code Playgroud)

首先,这是正确的方法吗?

让我们说回应如下:

{
  "args": {
            "foo": "bar"
          }
}
Run Code Online (Sandbox Code Playgroud)

为了访问密钥foo,我看到一个游乐场这样做:

result["args"].(map[string]interface{})["foo"]
Run Code Online (Sandbox Code Playgroud)

在这里,.()符号是什么?它是否正确?

json dictionary interface go

12
推荐指数
2
解决办法
2万
查看次数

如果在调用http.Get(url)时发生错误,是否需要关闭响应对象?

在以下代码中,还需要在错误情况下关闭响应主体:

res, err := http.Get(url)

if err != nil {
    log.Printf("Error: %s\n", err)
}

defer res.Body.Close()
Run Code Online (Sandbox Code Playgroud)

error-handling http go deferred

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

在缺少的地图键上去模板比较运算符

在尝试键入不存在键的映射时,我无法找到有关返回值类型的任何文档.从Go bug跟踪器看来它似乎是一种特殊的"无价值"

我正在尝试使用该eq函数比较两个值,但如果该键不存在则会出错

例:

var themap := map[string]string{}  
var MyStruct := struct{MyMap map[string]string}{themap}

{{if eq .MyMap.KeyThatDoesntExist "mystring"}}
  {{.}}
{{end}
Run Code Online (Sandbox Code Playgroud)

结果是 error calling eq: invalid type for comparison

从这里我假设nil值不是""Go 字符串中的空字符串.

有没有一种简单的方法来比较可能不存在的地图值和另一个值?

dictionary go go-templates

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

如何在Go中更新地图值

我想用字符串键和结构值构建一个映射,我可以使用它来更新map key标识的map中的struct值.

我已经试过不给我所需的输出.

我真正想要的是这个:

Received ID: D1 Value: V1
Received ID: D2 Value: V2
Received ID: D3 Value: V3
Received ID: D4 Value: V4
Received ID: D5 Value: V5

Data key: D1 Value: UpdatedData for D1
Data key: D2 Value: UpdatedData for D2
Data key: D3 Value: UpdatedData for D3
Data key: D4 Value: UpdatedData for D4
Data key: D5 Value: UpdatedData for D5

Data key: D1 Value: UpdatedData for D1
Data key: D2 Value: UpdatedData …
Run Code Online (Sandbox Code Playgroud)

struct dictionary go data-structures

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

如何告诉 json.Unmarshal 使用结构而不是接口

我想编写一个函数来接收几种类型的结构并从 JSON 中解组它们。为此,我有另一组带有预定义签名的函数,这些函数返回结构体实例,但由于每个函数返回不同类型的结构体,因此函数签名具有interface{}作为返回类型。

当我发送 json.Unmarshal 一个具体的结构时,它按我的预期工作,但是当我发送相同的结构时,interface{}它将它转换为地图。

这是描述问题的简化示例代码:

package main

import (
"encoding/json"
    "fmt"
)

type Foo struct {
    Bar string `json:"bar"`
}  

func getFoo() interface{} {
    return Foo{"bar"}
}

func main() {

    fooInterface := getFoo() 
    fooStruct := Foo{"bar"}
    fmt.Println(fooInterface) //{bar}
    fmt.Println(fooStruct)    //{bar}

    myJSON := `{"bar":"This is the new value of bar"}`
    jsonBytes := []byte(myJSON)

    err := json.Unmarshal(jsonBytes, &fooInterface )
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(fooInterface) //map[bar:This is the new value of bar] …
Run Code Online (Sandbox Code Playgroud)

json struct go unmarshalling

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

具有 const 或非常量值的 len() 的不同行为

下面的代码

const s = "golang.go"

var a byte = 1 << len(s) / 128
Run Code Online (Sandbox Code Playgroud)

的结果a是4。但是,更改const svar s如下之后

var s = "golang.go"

var a byte = 1 << len(s) / 128
Run Code Online (Sandbox Code Playgroud)

现在的结果a是0。

还有其他测试代码如下

const s = "golang.go"

var a byte = 1 << len(s) / 128     // the result of a is 4
var b byte = 1 << len(s[:]) / 128  // the result of b is 0

var ss = "golang.go" …
Run Code Online (Sandbox Code Playgroud)

constants bit-shift go

12
推荐指数
2
解决办法
1241
查看次数

在可变函数中混合"爆炸"切​​片和常规参数

我想知道为什么不可能在go中执行以下操作:

func main() {
    stuff := []string{"baz", "bla"}
    foo("bar", stuff...)
}

func foo(s ...string) {
    fmt.Println(s)
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,切片......"爆炸"切​​片,以便它可以用于多参数函数调用.所以上面的例子实际上应该扩展到foo("bar", "baz", "bla").

foo(stuff...) 按预期工作,这里没有惊喜,但在上面的例子中,编译器抱怨过多的参数.

这是一个理想的限制吗?我来自一个foo("bar", *stuff)非常好的红宝石背景(并且,至少在我的书中,同样的事情),这就是为什么这让我感到惊讶.

go slice

11
推荐指数
3
解决办法
2569
查看次数