1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 // The compiler knows that a print of a value of this type
12 // should use printhex instead of printuint (decimal).
15 func bytes(s
string) (ret
[]byte) {
16 rp
:= (*slice
)(unsafe
.Pointer(&ret
))
17 sp
:= stringStructOf(&s
)
26 // The compiler emits calls to printlock and printunlock around
27 // the multiple calls that implement a single Go print or println
28 // statement. Some of the print helpers (printsp, for example)
29 // call print recursively. There is also the problem of a crash
30 // happening during the print routines and needing to acquire
31 // the print lock to print information about the crash.
32 // For both these reasons, let a thread acquire the printlock 'recursively'.
36 mp
.locks
++ // do not reschedule between printlock++ and lock(&debuglock).
38 if mp
.printlock
== 1 {
41 mp
.locks
-- // now we know debuglock is held and holding up mp.locks for us.
47 if mp
.printlock
== 0 {
52 // write to goroutine-local buffer if diverting output,
53 // or else standard error.
54 func gwrite(b
[]byte) {
59 if gp
== nil || gp
.writebuf
== nil {
64 n
:= copy(gp
.writebuf
[len(gp
.writebuf
):cap(gp
.writebuf
)], b
)
65 gp
.writebuf
= gp
.writebuf
[:len(gp
.writebuf
)+n
]
76 func printbool(v
bool) {
84 func printfloat(v
float64) {
89 case v
+v
== v
&& v
> 0:
92 case v
+v
== v
&& v
< 0:
97 const n
= 7 // digits printed
123 for i
:= 0; i
< n
; i
++ {
133 // format +d.dddd+edd
134 for i
:= 0; i
< n
; i
++ {
136 buf
[i
+2] = byte(s
+ '0')
150 buf
[n
+4] = byte(e
/100) + '0'
151 buf
[n
+5] = byte(e
/10)%10
+ '0'
152 buf
[n
+6] = byte(e%10
) + '0'
156 func printcomplex(c complex128
) {
157 print("(", real(c
), imag(c
), "i)")
160 func printuint(v
uint64) {
163 for i
--; i
> 0; i
-- {
164 buf
[i
] = byte(v%10
+ '0')
173 func printint(v
int64) {
181 func printhex(v
uint64) {
182 const dig
= "0123456789abcdef"
185 for i
--; i
> 0; i
-- {
199 func printpointer(p unsafe
.Pointer
) {
200 printhex(uint64(uintptr(p
)))
203 func printstring(s
string) {
204 if uintptr(len(s
)) > maxstring
{
205 gwrite(bytes("[string too long]"))
211 func printslice(s
[]byte) {
212 sp
:= (*slice
)(unsafe
.Pointer(&s
))
213 print("[", len(s
), "/", cap(s
), "]")
214 printpointer(sp
.array
)
217 func printeface(e eface
) {
218 print("(", e
._type
, ",", e
.data
, ")")
221 func printiface(i iface
) {
222 print("(", i
.tab
, ",", i
.data
, ")")