我想让 X 个 goroutineCountValue使用并行性进行更新(numRoutines 与你拥有的 CPU 数量一样多)。
解决方案一:
func count(numRoutines int) (countValue int) {
var mu sync.Mutex
k := func(i int) {
mu.Lock()
defer mu.Unlock()
countValue += 5
}
for i := 0; i < numRoutines; i++ {
go k(i)
}
Run Code Online (Sandbox Code Playgroud)
它变成了数据竞争和返回的countValue = 0.
解决方案2:
func count(numRoutines int) (countValue int) {
k := func(i int, c chan int) {
c <- 5
}
c := make(chan int)
for i := 0; i < numRoutines; i++ { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用mongo-go-driver进行一些简单的操作。我在集合中插入一些数据,我希望它们在几秒钟后自动删除。
然后我在GO中写了一些东西,但它似乎没有按照我的预期工作。也许有些东西我没有得到,或者我做错了。
package main
import (
"bytes"
"context"
"fmt"
"log"
"text/tabwriter"
"time"
"github.com/Pallinder/go-randomdata"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx := context.TODO()
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Fatal(err)
}
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
db := client.Database("LADB")
col := db.Collection("LACOLL")
// add index to col
// the goal is to set a TTL for datas to only 1 secondes (test purpose)
model := mongo.IndexModel{
Keys: …Run Code Online (Sandbox Code Playgroud) 这是我的用户表:
| ID | 姓名 | 经过 |
|---|---|---|
| 1 | 测试 | 0a2f60e41b1d3d302c0af17bc65d4f48 |
| 2 | 安全用户 | 40597ff5ca18da3a91e0ee330496bc77 |
如何使用 GORM 获取所有行?如果我使用db.Find()方法,我只会得到第一行。
如何从 pod 获取事件消息,例如使用 client-go Kubernetes API 的命令:
kubectl describe pod spark-t2f59 -n spark
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 104s default-scheduler 0/19 nodes are available: 15 Insufficient cpu, 19 Insufficient memory.
Warning FailedScheduling 104s default-scheduler 0/19 nodes are available: 15 Insufficient cpu, 19 Insufficient memory.
Warning FailedScheduling 45s default-scheduler 0/20 nodes are available: 16 Insufficient cpu, 20 Insufficient memory.
Warning FailedScheduling 34s default-scheduler 0/20 nodes are available: 16 Insufficient cpu, 20 Insufficient memory. …Run Code Online (Sandbox Code Playgroud) 我正在解析用户发送的时间戳。时间戳是某个位置的本地时间戳,但源字符串未指定它。在服务器端,我正在查找该位置的时区,并且需要将时间转移到该时区,而不更改其显示值。
我知道我可以这样做,以便在不同的地点给我同等的时间:
package main
import (
"fmt"
"time"
)
func main() {
myTime := time.Now()
fmt.Println(myTime.Format(time.RFC3339))
loc, err := time.LoadLocation("America/New_York")
if err != nil {
panic(err)
}
fmt.Println(myTime.In(loc).Format(time.RFC3339))
}
Run Code Online (Sandbox Code Playgroud)
这只是打印:
2009-11-10T23:00:00Z
2009-11-10T18:00:00-05:00
Run Code Online (Sandbox Code Playgroud)
这不是我想要的。
我正在尝试找到一种将时区设置为 eg 的方法America/New_York,所以我应该得到 eg 2009-11-10T23:00:00-05:00,这是原始的当地时间,但应用了纽约偏移量。
我怎样才能在 Go 中做到这一点?
有人可以帮忙吗?
我有一个通过 exec.CommandContext 运行的应用程序(因此我可以通过 ctx 取消它)。除非出错,否则它通常不会停止。
我目前将其输出中继到 os.stdOut ,效果很好。但我也想通过通道获取每一行 - 这背后的想法是我将在该行上查找正则表达式,如果它为真,那么我将设置一个内部状态“ERROR”,例如。
虽然我无法让它工作,但我尝试了NewSscanner。这是我的代码。
正如我所说,它确实输出到 os.StdOut,这很棒,但我希望接收我设置的频道中发生的每一行。
有任何想法吗 ?
提前致谢。
func (d *Daemon) Start() {
ctx, cancel := context.WithCancel(context.Background())
d.cancel = cancel
go func() {
args := "-x f -a 1"
cmd := exec.CommandContext(ctx, "mydaemon", strings.Split(args, " ")...)
var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
lines := make(chan string)
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println("I am reading a line!")
lines <- scanner.Text()
}
}() …Run Code Online (Sandbox Code Playgroud) 我一直在阅读一些关于使用 golang 的 context 包的文章。我最近在博客中看到以下文章:http://p.agnihotry.com/post/understanding_the_context_package_in_golang/
这篇文章对 go 中的上下文取消函数做了以下说明:
“如果您愿意,您可以传递取消函数,但是,强烈不建议这样做。这可能会导致取消的调用者没有意识到取消上下文可能对下游产生什么影响。可能还会派生出其他上下文这可能会导致程序以意想不到的方式运行。简而言之,永远不要传递取消函数。”
但是,如果我希望激活父 context.Done() 通道,将取消函数作为参数传递似乎是唯一的选择(请参阅下面的代码片段)。例如,下面代码片段中的代码 Done 通道仅在执行 function2 时激活。
package main
import (
"context"
"fmt"
"time"
)
func function1(ctx context.Context) {
_, cancelFunction := context.WithCancel(ctx)
fmt.Println("cancel called from function1")
cancelFunction()
}
func function2(ctx context.Context, cancelFunction context.CancelFunc) {
fmt.Println("cancel called from function2")
cancelFunction()
}
func main() {
//Make a background context
ctx := context.Background()
//Derive a context with cancel
ctxWithCancel, cancelFunction := context.WithCancel(ctx)
go function1(ctxWithCancel)
time.Sleep(5 * time.Second)
go function2(ctxWithCancel, cancelFunction) …Run Code Online (Sandbox Code Playgroud) 我正在尝试访问触发我的 Golang 函数的 Pub/Sub 消息的 messageId。为此,我尝试修改Cloud Functions 文档中的 PubSubMessage 结构:
// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
type PubSubMessage struct {
Data []byte `json:"data"`
MessageId string `json:"messageId"`
}
Run Code Online (Sandbox Code Playgroud)
该函数编译正常,但 MessageID 值变为空。更改类型没有帮助。我想知道是否有办法从函数内获取触发消息 Id。或者也许根本没有传递给函数?
我用的是法因。我正在开发一款游戏(显然带有按钮)。我想知道是否有办法改变按钮的背景?我知道有一个带有图像而不是文本的按钮,但我只想更改按钮的背景颜色。
go ×10
channel ×1
client-go ×1
concurrency ×1
fyne ×1
go-context ×1
go-get ×1
go-gorm ×1
godoc ×1
kubernetes ×1
mongo-go ×1
mongodb ×1
timezone ×1
ttl ×1