我正在做Go之旅中的练习,但我遇到了一个我不能解决的问题。
我正在做,我得到了这个错误:
256 x 256
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
main.Pic(0x100, 0x100)
/tmp/sandbox1628012103/prog.go:14 +0xcf
golang.org/x/tour/pic.Show(0xc0000001a0)
/tmp/gopath962180923/pkg/mod/golang.org/x/tour@v0
在使用"make()“创建一个长度较大的值的动态数组时,我遇到了错误"panic: runtime error: makeslice: len out out range”。
例如:
arr := make([]int, length) //length is a dynamic value
我知道,这个问题已经在这里被问到了()。但是,make方法不支持golang中"int“数据类型的最大值。它们使用基于结构类型(大小)的长度值。是否有任何预定义的API可用来查找可以在golang中声明的集合的最大长度值?
例如:
maxInt := int(^uint(0)
我正在验证来自mariadb数据库的散列,但我不确定为什么必须获取数组的一部分,而不是只传递数组。 我在golang中对令牌进行散列,如下所示: // This type is a [32]uint8
tokenHash := sha256.Sum256(([]byte(token))) 然后,我尝试将令牌散列与此处保存在数据库中的散列进行匹配 // tokenHash[:] is of type []uint8
err := m.DB.QueryRowContext(ctx, query, tokenHash[:], time.Now()) 如果我没有获取切片,我得到的错误是:sql: c
从Golang中的切片创建自己的类型是一个好主意吗?
示例:
type Trip struct {
From string
To string
Length int
}
type Trips []Trip // <-- is this a good idea?
func (trips *Trips) TotalLength() int {
ret := 0
for _, i := range *trips {
ret += i.Length
}
return ret
}
在我的例子中创建像Trips
如何在golang中将动态数组结构作为参数传递
例如,用于
type Dog {
Name string
}
type Cat {
Name uint
}
我想要一个函数,它接受猫或狗的数组作为参数,并在函数中循环数组。下面是我想要实现的功能
func (array of either cats or dogs ){
for _ , item := range (array of either cats or dogs) {
}
}
我正在尝试简单地制作一个二维数组作为对象上的一个字段。我想要一个创建对象并返回它的函数。但是,当尝试创建2d数组时,它返回单个数组。
type Struct struct {
X int
Y int
Grid [][]bool
}
func NewLifeSimulator(sizeX, sizeY int) *Struct{
s := &Struct{
X: sizeX,
Y: sizeY,
Grid: make([][]bool, sizeX,sizeY), //Creates a re
我想创建一个动态初始化的位图数据结构。我正在尝试使用golang地图来实现此目的:
type Register map[bool]*[]bool
我初始化map的方式是:
register := make(Register)
register[true] := make(*[]bool, len(arr)) // arr is the array for which i want to create the bitmap
这显然会产生编译问题,错误如下:
error: cannot make type *[]bool
我也尝试过使用指针语法:
register[true] = &(m
我正在尝试使用“Go编程语言”学习Golang,我已经读到了切片部分。他们在数组和切片之间进行比较,因为两个数组可以与==进行比较,而两个切片不能。正文内容如下:
"== operator for arrays of strings, it may be puzzling that slice
comparisons do not also work this way. There are two reasons why deep
equivalence is problematic. First, unlike array elements, the elements
of a s
您有一个golang结构切片,您想要更改其中的一个条目。
type Type struct {
value int
}
func main() {
s := []Type{{0}, {0}}
// Prints [{0} {0}]
fmt.Println(s)
firstEntry := s[0]
firstEntry.value = 5
// Also prints [{0} {0}]
fmt.Println(s)
}
使用括号运算符并修改其返回值不会更改切片本身。
修改切片条目的推荐方式是什么?
在戈朗有许多使用nil的案例。例如:
func (u *URL) Parse(ref string) (*URL, error) {
refurl, err := Parse(ref)
if err != nil {
return nil, err
}
return u.ResolveReference(refurl), nil
}
但我们不能这样用它:
var str string //or var str int
str = nil
golang编译器将抛出一个can't use nil as type string in assig
如何在golang中通过...解压可变参数:
func run(a, b string, list ...int) {
}
func call(list ...int) {
run(x, y, list...) // use "list..." is not allowed
}
func main() {
a := []int{1, 2, 3}
call(a...)
}
为什么这是非法的?据我所知,list参数是一个可以由...解包的切片。
我正在将一个Yaml配置文件解组到Golang struct。我想实现以下逻辑: if blacklist key is not there in yaml:
then allow everything
else if blacklist key is there but there are no values:
then block everything
else if blacklist values are there in yaml:
then filter out only the listed items 我无法区分最后两种情况。本质上两者看起来是一样的,