我正在使用此包github.com/dgrijalva/jwt-go/v4在登录函数中设置声明:
now := time.Now()
claims := &jwt.StandardClaims{
Issuer: "Test",
ExpiresAt: now.Add(time.Hour * 24).Unix(),
}
Run Code Online (Sandbox Code Playgroud)
IDE 不断告诉我:
无法使用 'now.Add(time.Hour * 24).Unix()' (类型 int64)作为Time类型。
我读到,因为我输入了错误的值,但是,在我在网上看到的所有示例中,这正是大多数人的设置方式。
我仍在学习 go,所以我不确定将此时间格式转换为有效格式的正确方法。
我需要观察(并等待)直到 POD 被删除。我需要这样做是因为我需要在第一个 pod 被删除后立即启动第二个 pod(具有相同的名称)。
这就是我正在尝试的:
func (k *k8sClient) waitPodDeleted(ctx context.Context, resName string) error {
watcher, err := k.createPodWatcher(ctx, resName)
if err != nil {
return err
}
defer watcher.Stop()
for {
select {
case event := <-watcher.ResultChan():
if event.Type == watch.Deleted {
k.logger.Debugf("The POD \"%s\" is deleted", resName)
return nil
}
case <-ctx.Done():
k.logger.Debugf("Exit from waitPodDeleted for POD \"%s\" because the context is done", resName)
return nil
}
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于,当我获取Deleted事件时,是 POD 接收到要删除的事件时,而不是实际删除时。做了一些额外的测试,我用以下代码结束了调试过程:
case event := …Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
if cfg.GetRedisTLS() {
clientOpts.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
//nolint:gosec // need insecure TLS option for testing and development
InsecureSkipVerify: cfg.GetRedisInsecure(),
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行时,golangci-lint run它会识别该nolint指令并忽略该错误,但是当 Sonarqube 运行时,它会不断失败并显示消息“TLS InsecureSkipVerify 可能为真”
此问题https://github.com/securego/gosec/issues/278讨论了#nosec在评论中使用来禁用该错误。这里讨论了在声明的特定部分中使用它https://github.com/securego/gosec/issues/278#issuecomment-745209803
所以我尝试过:
if cfg.GetRedisTLS() {
clientOpts.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
//nolint:gosec // need insecure TLS option for testing and development
// NOSONAR #nosec
InsecureSkipVerify: cfg.GetRedisInsecure(),
}
}
Run Code Online (Sandbox Code Playgroud)
和
if cfg.GetRedisTLS() {
clientOpts.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
//nolint:gosec // need insecure TLS option …Run Code Online (Sandbox Code Playgroud) 我有一个带有登录端点的 REST API。登录端点接受用户名和密码,服务器通过发送包含一些有效负载(如 JWT)的 HTTPOnly Cookie 进行响应。
我一直使用的方法已经工作了几年,直到上周Set-Cookie标题停止工作。在 REST API 失去功能之前,我没有接触过它的源代码,因为我当时正在开发基于 Svelte 的前端。
我怀疑这与本地主机中Secure设置的属性有关。false然而,根据使用HTTP cookies,只要是本地主机,不安全的连接就应该没问题。我以这种方式开发 REST API 一段时间了,很惊讶地发现 cookie 不再被设置。
使用 Postman 测试 API 会产生设置 cookie 的预期结果。
我尝试重新创建真实 API 的一般流程,并将其精简为核心要素。
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/golang-jwt/jwt/v4"
)
const idleTimeout = 5 * time.Second
func main() {
app := fiber.New(fiber.Config{
IdleTimeout: idleTimeout,
})
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept, …Run Code Online (Sandbox Code Playgroud) 我最近正在学习 golang 中的并发,我正在考虑一个程序,生成一系列数字,然后将它们同时发送到三个切片。这是代码:
func main() {
ch := make(chan int)
done := make(chan bool)
var bag1 []int
var bag2 []int
var bag3 []int
go func() {
for i := 0; i < 1000000; i++ {
ch <- i
}
close(ch)
done <- true
}()
go sendToBag(&bag1, ch)
go sendToBag(&bag2, ch)
go sendToBag(&bag3, ch)
<-done
len1 := (len(bag1))
len2 := (len(bag2))
len3 := (len(bag3))
fmt.Println("length of bag1:", len1)
fmt.Println("length of bag2:", len2)
fmt.Println("length of bag3:", len3)
fmt.Println("total length:", len1+len2+len3)
}
func …Run Code Online (Sandbox Code Playgroud) 我想从 github 安装这个工具:https ://github.com/ethicalhackingplayground/ssrf-tool
我正在使用cmd: go install github.com/ethicalhackingplayground/ssrf-tool@latest
输出 :
go: finding module for package github.com/projectdiscovery/gologger
go: finding module for package github.com/briandowns/spinner
go: finding module for package github.com/logrusorgru/aurora
go: found github.com/briandowns/spinner in github.com/briandowns/spinner v1.18.1
go: found github.com/logrusorgru/aurora in github.com/logrusorgru/aurora v2.0.3+incompatible
go: found github.com/projectdiscovery/gologger in github.com/projectdiscovery/gologger v1.1.4
# github.com/ethicalhackingplayground/ssrf-tool
..\..\..\go\pkg\mod\github.com\ethicalhackingplayground\ssrf-tool@v0.0.0-20200901082948-7f3cffc3c7bb\ssrftool.go:34:2: undefined: gologger.Printf
..\..\..\go\pkg\mod\github.com\ethicalhackingplayground\ssrf-tool@v0.0.0-20200901082948-7f3cffc3c7bb\ssrftool.go:35:2: undefined: gologger.Infof
..\..\..\go\pkg\mod\github.com\ethicalhackingplayground\ssrf-tool@v0.0.0-20200901082948-7f3cffc3c7bb\ssrftool.go:36:2: undefined: gologger.Infof
Run Code Online (Sandbox Code Playgroud)
我对 golang 非常陌生,go 在我的系统中安装得很好,因为 github 上的其他工具运行良好。如果需要对该工具的代码进行一些更改,请提出建议。
当我从除 main 之外的其他 go 文件访问文件时,如何处理文件路径。
\n在 other.go 文件中,我尝试运行 ParseFS,但它给出了 template:pattern matches no files:templates/test.tmpl错误。这是我的文件树。
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 go.mod\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.go\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 other\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 other.go\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 templates\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.tmpl\nRun Code Online (Sandbox Code Playgroud)\n其他/其他.go
\npackage other\n\nimport (\n "embed"\n "fmt"\n "html/template"\n)\n\nvar templateFS embed.FS\n\nfunc Check() error {\n _, err := template.New("email").ParseFS(templateFS, "templates/"+ "test.tmpl")\n\n if err != nil {\n fmt.Println(err)\n }\n return nil\n}\nRun Code Online (Sandbox Code Playgroud)\n主/main.go
\nfunc main() {\n err :=othher.Check()\n\n if err != nil {\n fmt.Println(err)\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n 如何使用用户输入创建结构体数组?
我正在尝试创建一个循环,该循环将从用户那里获取结构的输入并将其添加到结构数组中
package main
import "fmt"
type person struct {
name string
age int
}
var n int
var p person
var list []person
func main() {
//Enter your code here. Read input from STDIN. Print output to STDOUT
fmt.Scanln(&n)
for i := 0; i < n; i++ {
var namez string
var numberz int
fmt.Scanln(&namez)
fmt.Scanln(&numberz)
list[i] = person{name: namez, age: numberz}
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以在 switch case 中使用 strings.Contains ?就像是:
func function(str string){
switch str {
case "str1":
...
case strings.Contains("test"):
...
default:
...
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:它是一个例子,这不是我需要它的真实情况。我正在过滤许多结果,我想要包含 X 的所有结果,并且我也有需要完全匹配的案例。
我正在尝试使用断点调试 VSCode 中的一个特定单元测试。
在命令行上,这可以完美地执行:
go test -v ./cmd/bacalhau -run TestCommands
Run Code Online (Sandbox Code Playgroud)
但是,当我选择以下命令运行时,我得到以下信息:
{
"name": "Debug Specific Test",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/bacalhau/devstack_test.go",
"args": [
"-test.run",
"TestCommands"
],
"env": {
"LOG_LEVEL": "debug"
},
"showLog": true
}
Run Code Online (Sandbox Code Playgroud)
输出:
Starting: /home/user/go/bin/dlv dap --check-go-version=false --log=true --log-output=debugger --listen=127.0.0.1:43095 --log-dest=3 from /home/user/code/bacalhau
DAP server listening at: 127.0.0.1:43095
2022-03-12T19:31:11Z info layer=debugger launching process with args: [/home/user/code/bacalhau/__debug_bin -test.run TestCommands]
2022-03-12T19:31:11Z error layer=debugger can't find build-id note on binary
Type 'dlv help' for list of commands.
2022-03-12T19:31:12Z …Run Code Online (Sandbox Code Playgroud) go ×10
concurrency ×1
contains ×1
cookies ×1
debugging ×1
express ×1
file ×1
go-templates ×1
gosec ×1
httponly ×1
jwt ×1
kubernetes ×1
lint ×1
string ×1
svelte ×1
websecurity ×1