- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
代码A:
package main
import "fmt"
func main() {
slice := IntSlice{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(slice)
}
type IntSlice []int
输出A:
[0 1 2 3 4 5 6 7 8 9]
代码 B:
package main
import "fmt"
func main() {
slice := IntSlice{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(slice)
}
type IntSlice []int
func (slice IntSlice) Error() string { return "this is called." }
输出B:
this is called.
为什么 fmt.Println(slice)
的行为对于这两个代码(A 和 B)不同?
或者为什么 fmt.Println(slice)
会自动调用 slice.Error()
?
最佳答案
这是 documented作为 fmt
行为的一部分(强调我的):
Except when printed using the verbs %T and %p, special formatting considerations apply for operands that implement certain interfaces. In order of application:
If the operand is a reflect.Value, the operand is replaced by the concrete value that it holds, and printing continues with the next rule.
If an operand implements the Formatter interface, it will be invoked. Formatter provides fine control of formatting.
If the %v verb is used with the # flag (%#v) and the operand implements the GoStringer interface, that will be invoked.
If the format (which is implicitly %v for Println etc.) is valid for a string (%s %q %v %x %X), the following two rules apply:
If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).
If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).
For compound operands such as slices and structs, the format applies to the elements of each operand, recursively, not to the operand as a whole. Thus %q will quote each element of a slice of strings, and %6.2f will control formatting for each element of a floating-point array.
fmt
包看到 slice
实现了 error
并打印了 value.Error()
,而不是迭代在 slice 上并将格式应用于每个元素。
关于go - 为什么在 Golang 中使用 fmt.Println(slice) 打印 slice 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38361397/
在我们的项目中,我们决定在我们的项目中使用最新的 fmt 版本 (6.2.0) 并主要使用 printf 功能,因为我们在广泛使用 printf 的地方进行日志记录。 我使用 fmt 包中包含的 CM
JSTL 标签用来绑定数据源(.properties)文件,JSTL 标签用来从指定的资源文件中调用信息。 语法 JSP 标签的语法如下: 代码块 其中: basename:指定要
我有以下 JSTL: enumValue.reputation 给出“非常好”、“完美”等字符串,对于这种情况,我需要将其小写“非常好”、“完美”。怎么把它变成小写?
为什么是这样fmt::format_to(OutputIt, ...)而不是 fmt::print(OutputIt, ...) ?? 我目前正在熟悉 {fmt} , 一个/现代 C++ 格式库。 在
所以我目前正在开始使用 Rust,并且正在阅读我的 Rust By Example。 做练习,边玩边玩代码。 但在 RBE Display函数描述它将 Fmt::Display 描述为比 Fmt::D
我不确定如何命名这个问题,因为我是 Rust 新手,所以请随意提出修改建议。 我有两个结构。一个是 Job 结构,其中包含一些数字,例如作业需要多长时间等。另一个是 JobSequence,其中包含
我不确定如何命名这个问题,因为我是 Rust 新手,所以请随意提出修改建议。 我有两个结构。一个是 Job 结构,其中包含一些数字,例如作业需要多长时间等。另一个是 JobSequence,其中包含
关于Golang中的标准I\O,网上所有的教程,无一异常(exception)地讨论和使用bufio,我测试过,效果很好。 但我的问题是关于使用 fmt 包读取标准输入,这要么给我错误,要么陷入循环。
正如标题,我很难描述这个问题。所以我在下面展示了示例代码、输入和输出。 package main import ( "fmt" ) func main() { for i := 1;
我正在尝试在我的 JSP Web 应用程序上本地化货币,问题是当我要求语言环境时,我只得到语言代码(“en”)而不是完整的语言和国家代码(“en_US”)。问题是,当 setLocale 的值不包含语
我使用的是 MACOS Mojave 版本 10.14.3,需要使用 GNU 编译器而不是 clang。 我使用 brew install gcc 安装了 gcc 编译器.然后我使用 brew ins
我正在尝试编译一些代码,但出现了一个非常奇怪的错误: the trait core::fmt::Show is not implemented for the type core::fmt::Show
我是 Go 语言和编程的新手。 谁能解释错误的含义以及我该如何解决? 最佳答案 使用 fmt.Println 而不是 fmt.PrintLn。将字母 L 小写。 关于go - undefined :
package main import ( "fmt" ) func main() { fmt.Printf("%c, %x, %x", 'ᚵ', 'ᚵ', "ᚵ") } 输出: ᚵ,
每当我在 Windows cmd 上的 Go 中运行 fmt.Print("\033c") 时,它不会清除屏幕,而是在我执行 console 时打印 c。 log("\033c") 在 javascr
我正在使用字典进行一些测试,为此,我以一种格式从数据库中打印出我想要的 Float64 值,以便将它们复制并粘贴到我的测试结构数组中,但是当我的测试失败时,我注意到这些值不同,但仅相差 0.00000
我目前有一个项目是这样组织的: ~/code/go /bin /pkg /src /proj/main.go
我目前正在做一个 spring 项目,我不得不在我的 JSP 中使用 fmt 标签。事实上 fmt 标签对我来说工作正常,它从 messages.properties 读取正确的值文件。 例如: 在
我想知道{fmt}库是否只允许修改包含多个位置参数的字符串中的一个位置参数? 这是我的测试不起作用,{fmt}文档未显示解决方案 std::string text = "{name} is {nb}
Closed. This question is not reproducible or was caused by typos。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-to
我是一名优秀的程序员,十分优秀!