假设我每 2 秒执行一次查询,我应该在每个请求上打开连接,还是应该保持连接处于活动状态直到应用程序(服务器)停止?
我有一个struct名为的用户包user
type Account struct {
Tp string `json:"type"bson:"type"`
AccountId string `json:"account_id"bson:"account_id"`
Credentials map[string]interface{} `json:"credentials,omitempty"bson:"credentials,omitempty"`
ProfilePicture string `json:"profile_picture,omitempty"`
Username string `json:"username"bson:"username"`
AccessToken map[string]interface{}`bson:"access_token,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)
并account试图将此帐户结构嵌入另一个结构中
type returnAccount struct {
user.Account
AccessToken string `json:"access_token,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)
用户包在尝试嵌入之前已正确导入我正在成功使用它
最后在一个循环我得到用户帐户,并制作一个地图user/accounts和从我的功能返回这里是我的功能
func getAccounts(usr *user.AuthenticatedUser, id ...string) (accounts map[string]returnAccount) {
accounts = make(map[string]returnAccount)
if len(id) > 0 {
for _, v := range id {
for _, acnt := range usr.Accounts {
if acnt.AccountId == v {
accounts[acnt.AccountId] = returnAccount{ …Run Code Online (Sandbox Code Playgroud) 我试图通过注释掉一个函数并查看它如何影响结果来调试程序。但是注释掉这个函数意味着它未被使用,这会导致 Go 抛出“未使用的函数”错误。如何暂时禁用此错误,以便可以调试我的程序,而不必仅仅为了调试一小部分而重写整个程序?
我知道可以忽略未使用的导入和变量(详细信息如下),但找不到有关忽略未使用函数的任何内容。
要禁用/忽略未使用的导入_错误,只需在包名称前面添加一个即可。
import (
"fmt" // how you normally import packages
_"log" // how you import packages and ignore the unused import error
)
Run Code Online (Sandbox Code Playgroud)
要禁用/忽略未使用的变量,您可以重新命名该变量_。
myvar, _ := some_function()
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) 我有一个项目,需要在 EC2 重新启动时运行命令。我只找到了有关用户数据的信息,但仅在首次启动时有效,这并不完全是我所需要的。每次连接到机器时我都需要运行一个命令。
该程序的目的是解码嵌入有“embed”的img。图像 ( ) 与main.gobu.png位于同一目录中。
package main
import (
"bytes"
_ "embed"
"image"
)
var (
//go:embed bu.png
img []byte
)
func main() {
a := bytes.NewBuffer(img)
a, b, e := image.Decode()
println(e.Error())
// image: unknown format
println(b)
//
println(a)
// (0x0,0x0)
// println(string(img))
// the text of the image seem a little different between nano
}
Run Code Online (Sandbox Code Playgroud)
图像数据应位于 img 变量中,导致“嵌入”导入
go ×4
amazon-ec2 ×1
compilation ×1
connection ×1
decode ×1
go-context ×1
image ×1
inheritance ×1
linux ×1
mysql ×1
struct ×1