我正在尝试从字符串数组中替换特定位置字符。这是我的代码的样子:
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。
我有以下代码:
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
我有一个带有字段的结构:
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)
我的内存占用应该是多少?当我用空字段实例化一个结构体时,我是否仍然使用内存?
我很确定我是,但我只需要确认。
我怎么能做这样的事情?
我试图将 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 …
就我而言,我有 2 个数据类型为*uint64和 的变量*uint32。我需要将它们转换为int数据类型。
当我尝试用int()函数转换它们时,它会引发这样的错误:
无法将 *uint64 类型的表达式转换为 int 类型。
我注意到,int()如果数据类型没有字符,相同的函数可以正常工作* (asterisk)。
所以我的问题是如何正确地将数据类型转换*uint64为*uint32数据int类型?!
我编写了以下代码来获取数组中的下一项。
count:=len(value.Values)
for index, currentRow := range value.Values {
var nextRow Value
if index< count{
nextRow = value.Values[index+1]
fmt.Print(nextRow)
}
}
Run Code Online (Sandbox Code Playgroud)
运行上面的代码时我感到恐慌。
Goroutine 恐慌:运行时错误:索引超出范围
知道如何从切片中获取下一个项目。
我想以JSON格式获取CF命令的输出,但是我不确定该使用json.Marshal还是json.MarshalIndent。
我需要的输出是这样的:
{
"key1": "value1",
....
"keyn": "valuen",
}
Run Code Online (Sandbox Code Playgroud)
这是旧示例,但不是所需的输出:
cmd.ui.Say(terminal.EntityNameColor(T("User-Provided:")))
for _, key := range keys {
// cmd.ui.Say("%s: %v", key, envVars[key])
here needed a new one with json.marshalIdent
}
Run Code Online (Sandbox Code Playgroud)
我从来没有使用过go,所以我真的不知道该使用哪个以及如何使用。
我在这里迷失了方向,我试图让一个goroutine添加到数组中,并从中读取另一个goroutine,我怀疑它与下面的内容有些相似,但我需要尝试使用wait() 。
但是,我遇到了错误prog.go:19:14: too many variables in range,第19行是for _, v := range c {我在网上找不到答案,我在做什么或不做什么?
package main
import (
"fmt"
//"time"
"sync"
)
func hello(wg *sync.WaitGroup, s []int, c chan int) {
for _, v := range s {
c <- v
}
fmt.Println("Finished adding to channel")
wg.Done()
}
func hello2(wg *sync.WaitGroup, c chan int) {
fmt.Println("Channel",c)
for _, v := range c {
fmt.Println("Received",v)
}
fmt.Println("Finished taking from channel")
wg.Done()
}
func main() {
s := …Run Code Online (Sandbox Code Playgroud) 让我们说在第三方库中我们有一个接口和一个实现这个接口的结构.我们还假设有一个函数将ParentInterface作为参数,它对不同类型具有不同的行为.
type ParentInterface interface {
SomeMethod()
}
type ParentStruct struct {
...
}
func SomeFunction(p ParentInterface) {
switch x := p.Type {
case ParentStruct:
return 1
}
return 0
}
Run Code Online (Sandbox Code Playgroud)
在我们的代码中,我们想要使用这个接口,但是使用我们的增强行为,所以我们将它嵌入到我们自己的结构中.编译器实际上允许我们ParentInterface直接在我的struct上调用函数:
type MyStruct struct {
ParentInterface
}
parentStruct := ParentStruct{...}
myStruct := MyStruct{parentStruct}
parentStruct.SomeMethod() // Compiler OK.
myStruct.SomeMethod() // Compiler OK. Result is same. Great.
SomeFunction(parentStruct) // Compiler OK. Result is 1.
SomeFunction(myStruct.ParentInterface) // Compiler OK. Result is 1.
SomeFunction(myStruct) // Compiler OK. Result is 0. (!)
Run Code Online (Sandbox Code Playgroud)
最后一例不是问题吗?我不止一次遇到过这种错误.因为我愉快地使用 …
在 Go 中将 "" 存储为映射键是好还是坏的做法?这似乎是一种特殊情况,不适合作为钥匙存储。你怎么认为?