我刚才有一个问题,我有一个结构数组,例如
package main
import "log"
type Planet struct {
Name string `json:"name"`
Aphelion float64 `json:"aphelion"` // in million km
Perihelion float64 `json:"perihelion"` // in million km
Axis int64 `json:"Axis"` // in km
Radius float64 `json:"radius"`
}
func main() {
var mars = new(Planet)
mars.Name = "Mars"
mars.Aphelion = 249.2
mars.Perihelion = 206.7
mars.Axis = 227939100
mars.Radius = 3389.5
var earth = new(Planet)
earth.Name = "Earth"
earth.Aphelion = 151.930
earth.Perihelion = 147.095
earth.Axis = 149598261
earth.Radius = 6371.0
var venus = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试从漂亮的 Logrus(对调试非常有帮助)迁移我的应用程序并引入 Uber 日志框架 Zap。
使用 Logrus,我只能初始化记录器一次并从其他 Go 文件中重用它,例如:
package main
import(
// Print filename on log
filename "github.com/onrik/logrus/filename"
// Very nice log library
log "github.com/sirupsen/logrus"
)
func main(){
// ==== SET LOGGING
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "Jan _2 15:04:05.000000000"
Formatter.FullTimestamp = true
Formatter.ForceColors = true
log.AddHook(filename.NewHook()) // Print filename + line at every log
log.SetFormatter(Formatter)
}
Run Code Online (Sandbox Code Playgroud)
从其他 Go 文件中,我可以重用该记录器而无需任何其他初始化:
// VerifyCommandLineInput is delegated to manage the inputer parameter provide with the input flag from command line
func VerifyCommandLineInput() …
Run Code Online (Sandbox Code Playgroud) 场景:
Go 1.18 支持泛型
问题:
无法将数字转换为泛型。
说明:
我正在尝试移植我的general purpose
库以支持泛型。我正在处理数字转换错误。
我定义了一个包含所有数字类型的包,如下所示:
package types
type Number interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
}
Run Code Online (Sandbox Code Playgroud)
我在处理泛型时没有什么特别的问题。我唯一不明白的是:
如何将定义的类型(如int
)转换为泛型?
让我们假设以下示例:
// FindIndexValue is delegated to retrieve the index of the given value into the input array.
func FindIndexValue[T types.Number](array []T, value T) []T {
var indexs []T
for …
Run Code Online (Sandbox Code Playgroud) 请您帮我了解如何从数组生成切片。为什么 ID1 和 ID2 不一样?
a := [2]string{"a", "b"}
b := [2]string{"c", "d"}
var z [2][2]string
z[0] = a
z[1] = b
fmt.Printf("%s\n", z)
var id [][]string
for _, t := range z {
temp := t[:]
id = append(id, temp)
}
fmt.Printf("\nid1 = %s", id)
var id2 [][]string
for _, t := range z {
temp := t
id2 = append(id2, temp[:])
}
fmt.Printf("\nid2 = %s", id2)
Run Code Online (Sandbox Code Playgroud)
[[A B C D]]
id1 = [[cd] [cd]]
id2 = [[ab] [cd]]
有没有办法将引号字符的混合存储为 Redis 中的值?我的具体需求是存储 PHP 和 HTML 字符串,并保持引用类型的混合。
在Python中:
import redis
db = redis.Redis('localhost')
phpfile = /root/test.php
with open(phpfile) as f:
bar = f.read()
db.set('foo', bar)
Run Code Online (Sandbox Code Playgroud)
如果我尝试以任何其他直接方式传递字符串,它当然会失败,因为所有竞争的引号。
编辑:
我要结束这个问题。我无法使用上面的代码在 Python 中重复该行为,因此这一定是我代码中另一层的问题,而不是 Redis 的问题。
使用最新版本logrus
(v1.4.2),我无法在日志格式化程序中设置毫秒/微秒。
对于以前的版本(我不记得是哪个),我只是使用以下时间格式:
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "15-01-2018 15:04:05.000000"
Formatter.FullTimestamp = true
Formatter.ForceColors = true
log.SetFormatter(Formatter)
log.SetLevel(log.DebugLevel)
Run Code Online (Sandbox Code Playgroud)
不幸的是,在最新版本中,似乎不再允许这种时间戳格式。
当我尝试使用上述时间格式打印一行时,我收到以下结果:
17-11-7118 17:35:46.314715
而不是正确的:
7-11-2019 17:35:46.314715
有人知道如何配置以微秒/毫秒Logrus
精度打印时间戳吗?