例如,我有一个字符串,由"sample.zip"组成.如何使用strings包或其他方法删除".zip"扩展名?
我最近拿起了Go,现在我对以下代码感到困惑:
package main
import "fmt"
func main() {
a := make([]int, 5)
printSlice("a", a)
b := make([]int, 0, 5)
printSlice("b", b)
c := b[:2]
printSlice("c", c)
d := c[2:5]
printSlice("d", d)
}
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
Run Code Online (Sandbox Code Playgroud)
结果如下:
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
c len=2 cap=5 [0 0] //why the capacity of c not 2 but 5 instead
d len=3 cap=3 [0 0 …Run Code Online (Sandbox Code Playgroud) 只想知道使用fmt软件包功能的打印格式列表.
例如,像:
%v用于打印值.%T可以打印值的类型.
还有什么?
我在实现以下代码时遇到错误:
package main
import (
"fmt"
)
type Struct struct {
a int
b int
}
func Modifier(ptr *Struct, ptrInt *int) int {
*ptr.a++
*ptr.b++
*ptrInt++
return *ptr.a + *ptr.b + *ptrInt
}
func main() {
structure := new(Struct)
i := 0
fmt.Println(Modifier(structure, &i))
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个关于"ptr.a的无效间接(类型int)......"的错误.还有为什么编译器不给我关于ptrInt的错误?提前致谢.