小编icz*_*cza的帖子

复制功能如何工作?

我不明白该copy功能如何根据文档工作:

复制内置函数将元素从源切片复制到目标切片.(作为一种特殊情况,它还会将字符串中的字节复制到一个字节片段.)源和目标可能重叠.Copy返回复制的元素数,这将是len(src)和len(dst)的最小值.

func copy(dst, src []Type) int
Run Code Online (Sandbox Code Playgroud)

copy go slice

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

Go中的交互式网页

您知道是否可以在Go中创建交互式网页?例如,有一个或多个按钮,还是一个组合框,刷新页面时根据选择过滤数据?我试图寻找它,但没有发现任何相关的东西.

提前致谢.

webpage interactive go web server

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

GoLang,如果condtion如何写一个多行语句

我想匹配波纹管a与价值b,c,d,e,f的一次,而不是写多次这样.

我的价值观是:

a = 11
b = 22
c = 33
d = 44
e = 55
f = 66

if a != b && a != c && a != d && a != e && a != f{
    // Do something
} else{
    // Do something else
}
Run Code Online (Sandbox Code Playgroud)

是我的实际工作代码方法.

但是我想写它

if a != b or c or d or e or f {print text}
Run Code Online (Sandbox Code Playgroud)

a值应该在if语句中使用一次.有没有简单的方法?

comparison if-statement go

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

如何在Go中使用map作为数据载体?

我不确定正确的术语,但我如何使用它:

type MyType map[string]string
Run Code Online (Sandbox Code Playgroud)

作为"数据载体"(或OOP中的对象)?

这不起作用:

func NewMyType() *MyType {
    return make(MyType)
}
Run Code Online (Sandbox Code Playgroud)

我确实想使用指针,但显然这不起作用,编译器期望返回时引用.

constructor dictionary types pointers go

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

为什么cap不打印基础数组的长度?

我正在阅读“ 切片之旅”,在“ 切片长度和容量”部分中,运行了示例:

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    printSlice(s)

    // Slice the slice to give it zero length.
    s = s[:0]
    printSlice(s)

    // Extend its length.
    s = s[:4]
    printSlice(s)

    // Drop its first two values.
    s = s[2:]
    printSlice(s)
}

func printSlice(s []int) {
    fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
Run Code Online (Sandbox Code Playgroud)

当我构建应用程序并运行它时,它显示如下:

len=6 cap=6 [2 3 5 7 11 13]
len=0 cap=6 []
len=4 cap=6 [2 3 …
Run Code Online (Sandbox Code Playgroud)

arrays go slice

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

在 golang 中替换字符串中的字符

我正在尝试从字符串数组中替换特定位置字符。这是我的代码的样子:

package main

import (
    "fmt"
)

func main() {
    str := []string{"test","testing"}
    str[0][2] = 'y'
    fmt.Println(str)
}
Run Code Online (Sandbox Code Playgroud)

现在,运行它给了我错误:

cannot assign to str[0][2]
Run Code Online (Sandbox Code Playgroud)

知道如何做到这一点吗?我曾尝试使用 strings.Replace,但 AFAIK 它将替换给定字符的所有出现,而我想替换该特定字符。任何帮助表示赞赏。TIA。

string replace go

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

为什么我的`for-select`语句即使关闭后仍会继续从我的频道接收?

我有以下代码:

package main

import (
    "fmt"
    "time"
)

func main() {
    ch := make(chan int)
    ch2 := make(chan int)
    go func(c chan int, c2 chan int) {
        for {
            select {
            case v := <-c:
                fmt.Println(v)
            case v := <-c2:
                fmt.Println(v)
            default:
            }
        }
    }(ch, ch2)
    ch <- 1
    close(ch)
    close(ch2)
    time.Sleep(10 * time.Second)
}
Run Code Online (Sandbox Code Playgroud)

当我运行此命令时,它会打印1到标准输出,然后继续打印0。为什么是这样?

我知道我可以在goroutine中检查通道是否关闭,但是我只是想知道原因。

另外,假设我要在关闭所有(多个)通道后从goroutine退出,这可能吗?我以为一旦关闭了所有通道,在所有通道都关闭后,在默认情况下我有可能退出goroutine

for-loop channel go

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

结构中的空字段是否消耗内存?

我有一个带有字段的结构:

type Measure struct {
    ID            int
    IndexName     string    
    IndexValue    int   
    Redistributed float64
    MyArray       []myObject
}
Run Code Online (Sandbox Code Playgroud)

如果我用

measure := Measure{
   ID: 10,
   IndexName: "",
   IndexName: 0,
   Redistributed: 0
   MyArray: nil         
}
Run Code Online (Sandbox Code Playgroud)

我的内存占用应该是多少?当我用空字段实例化一个结构体时,我是否仍然使用内存?

我很确定我是,但我只需要确认。

memory struct go

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

如何将结构作为参数传递给函数

我怎么能做这样的事情?

我试图将 astruct作为参数传递给Go 中的函数。

func handleEntityProperties(w http.ResponseWriter, r *http.Request) {
    const sliceSize = 100
    var entityProperties struct {
        Instance string `json:"instance"`
        Entities []struct {
            Id         string            `json:"id"`
            Properties map[string]string `json:"properties"`
            Type       string            `json:"type"`
        } `json:"entities"`
    }

    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        panic(err)
    }

    if !json.Valid([]byte(body)) {
        fmt.Fprintf(w, "invalid json")
        return
    }

    err = json.Unmarshal(body, &entityProperties)

    sendData(entityProperties.Entities[0:100])

    return
}

func sendData(entities struct) {
    log.Println("Doing things with entities ", entities)
}
Run Code Online (Sandbox Code Playgroud)

正如您在代码中看到的,我试图将 的前 100 个元素发送entityProperties.Entities …

json struct types go slice

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

如何将 *uint64 和 *uint32 数据类型转换为 int 数据类型?

就我而言,我有 2 个数据类型为*uint64和 的变量*uint32。我需要将它们转换为int数据类型。

当我尝试用int()函数转换它们时,它会引发这样的错误:

无法将 *uint64 类型的表达式转换为 int 类型。

我注意到,int()如果数据类型没有字符,相同的函数可以正常工作* (asterisk)

所以我的问题是如何正确地将数据类型转换*uint64*uint32数据int类型?!

types pointers type-conversion go

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