小编icz*_*cza的帖子

在Go中连接两个切片

我正在尝试将切片[1, 2]和切片组合在一起[3, 4].我怎么能在Go中这样做?

我试过了:

append([]int{1,2}, []int{3,4})
Run Code Online (Sandbox Code Playgroud)

但得到了:

cannot use []int literal (type []int) as type int in append
Run Code Online (Sandbox Code Playgroud)

但是,文档似乎表明这是可能的,我错过了什么?

slice = append(slice, anotherSlice...)
Run Code Online (Sandbox Code Playgroud)

append variadic-functions go slice

411
推荐指数
8
解决办法
23万
查看次数

Go中的标签有什么用途?

Go语言规范中,它提到了标签的简要概述:

字段声明后面可以跟一个可选的字符串文字标记,该标记成为相应字段声明中所有字段的属性.标签通过反射界面可见,但否则将被忽略.

// A struct corresponding to the TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers.
struct {
  microsec  uint64 "field 1"
  serverIP6 uint64 "field 2"
  process   string "field 3"
}
Run Code Online (Sandbox Code Playgroud)

这是一个非常简短的解释IMO,我想知道是否有人可以提供我这些标签的用途?

reflection struct go

364
推荐指数
3
解决办法
8万
查看次数

在Go中将字符串转换为整数类型?

我试图从返回的字符串转换flag.Arg(n)int.在Go中这样做的惯用方法是什么?

string type-conversion go

212
推荐指数
4
解决办法
17万
查看次数

如何以一种不会最终替换另一种的方式替换两个字符串?

假设我有以下代码:

String word1 = "bar";
String word2 = "foo";
String story = "Once upon a time, there was a foo and a bar."
story = story.replace("foo", word1);
story = story.replace("bar", word2);
Run Code Online (Sandbox Code Playgroud)

此代码运行后,story将是值"Once upon a time, there was a foo and a foo."

如果我以相反的顺序替换它们,则会出现类似的问题:

String word1 = "bar";
String word2 = "foo";
String story = "Once upon a time, there was a foo and a bar."
story = story.replace("bar", word2);
story = story.replace("foo", word1);
Run Code Online (Sandbox Code Playgroud)

价值story将是"Once …

java string replace

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

为什么多次添加0.1会保持无损?

我知道0.1十进制数不能用有限的二进制数(解释)精确表示,所以double n = 0.1会失去一些精度而不会完全正确0.1.另一方面0.5可以完全表示,因为它是0.5 = 1/2 = 0.1b.

已经说过,添加0.1 三次不会完全给出0.3以下代码打印是可以理解的false:

double sum = 0, d = 0.1;
for (int i = 0; i < 3; i++)
    sum += d;
System.out.println(sum == 0.3); // Prints false, OK
Run Code Online (Sandbox Code Playgroud)

但是,如何增加0.1 五次才会给出确切的答案0.5呢?以下代码打印true:

double sum = 0, d = 0.1;
for (int i = 0; i < 5; i++)
    sum += …
Run Code Online (Sandbox Code Playgroud)

java floating-point precision double

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

声明一个常量数组

我试过了:

const ascii = "abcdefghijklmnopqrstuvwxyz"
const letter_goodness []float32  = { .0817,.0149,.0278,.0425,.1270,.0223,.0202, .0609,.0697,.0015,.0077,.0402,.0241,.0675, .0751,.0193,.0009,.0599,.0633,.0906,.0276, .0098,.0236,.0015,.0197,.0007 }
const letter_goodness  = { .0817,.0149,.0278,.0425,.1270,.0223,.0202, .0609,.0697,.0015,.0077,.0402,.0241,.0675, .0751,.0193,.0009,.0599,.0633,.0906,.0276, .0098,.0236,.0015,.0197,.0007 }
const letter_goodness = []float32 { .0817,.0149,.0278,.0425,.1270,.0223,.0202, .0609,.0697,.0015,.0077,.0402,.0241,.0675, .0751,.0193,.0009,.0599,.0633,.0906,.0276, .0098,.0236,.0015,.0197,.0007 }
Run Code Online (Sandbox Code Playgroud)

首先,声明和初始化工作正常,但第二,第三和第四不起作用.如何声明和初始化const数组的浮点数?

arrays const go

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

static修饰符如何影响此代码?

这是我的代码:

class A {
    static A obj = new A();
    static int num1;
    static int num2=0;

    private A() {
        num1++;
        num2++;
    }
    public static A getInstance() {
        return obj;
    }
}

public class Main{
    public static void main(String[] arg) {
        A obj = A.getInstance();
        System.out.println(obj.num1);
        System.out.println(obj.num2);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是1 0,但我无法理解.

有人可以向我解释一下吗?

java static

109
推荐指数
5
解决办法
3246
查看次数

什么构建构建?

New Go程序员经常不知道或混淆基本的go build命令所做的事情.

构造go buildgo install命令的确切内容以及它们将结果/输出放在何处?

compilation build go

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

如何在Go中使用文字*int64?

我有一个带*int64字段的结构类型.

type SomeType struct {
    SomeField *int64
}
Run Code Online (Sandbox Code Playgroud)

在我的代码中的某个时刻,我想声明一个这样的文字(比如,当我知道所述值应为0,或者指向0时,你知道我的意思)

instance := SomeType{
    SomeField: &0,
}
Run Code Online (Sandbox Code Playgroud)

...除了这不起作用

./main.go:xx: cannot use &0 (type *int) as type *int64 in field value
Run Code Online (Sandbox Code Playgroud)

所以我试试这个

instance := SomeType{
    SomeField: &int64(0),
}
Run Code Online (Sandbox Code Playgroud)

......但这也行不通

./main.go:xx: cannot take the address of int64(0)
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?我能想出的唯一解决方案是使用占位符变量

var placeholder int64
placeholder = 0

instance := SomeType{
    SomeField: &placeholder,
}
Run Code Online (Sandbox Code Playgroud)

注意:当它是*int而不是a时,&0语法工作正常*int64.编辑:不,不.为此表示歉意.

编辑:

显然,我的问题含糊不清.我正在寻找一种方法来说明一个*int64.这可以在构造函数中使用,或者用于表示文字结构值,或者甚至作为其他函数的参数.但帮助函数或使用不同的类型不是我正在寻找的解决方案.

struct pointers literals go

79
推荐指数
3
解决办法
4万
查看次数

如何在Golang中打破一长串代码?

来自Python,我不习惯看到超过80列的代码行.所以当我遇到这个时:

err := database.QueryRow("select * from users where user_id=?", id).Scan(&ReadUser.ID, &ReadUser.Name, &ReadUser.First, &ReadUser.Last, &ReadUser.Email)
Run Code Online (Sandbox Code Playgroud)

我试图打破它

err := database.QueryRow("select * from users where user_id=?", id) \
    .Scan(&ReadUser.ID, &ReadUser.Name, &ReadUser.First, &ReadUser.Last, &ReadUser.Email)
Run Code Online (Sandbox Code Playgroud)

但我明白了

 syntax error: unexpected \
Run Code Online (Sandbox Code Playgroud)

我也试过打破输入并在结尾处添加分号:

err := database.QueryRow("select * from users where user_id=?", id) 
.Scan(&ReadUser.ID, &ReadUser.Name, &ReadUser.First, &ReadUser.Last, &ReadUser.Email);
Run Code Online (Sandbox Code Playgroud)

但我再次得到:

syntax error: unexpected .
Run Code Online (Sandbox Code Playgroud)

所以我想知道这样做的golangic方式是什么?

syntax go

79
推荐指数
4
解决办法
4万
查看次数