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.
9 // The compiler knows that a print of a value of this type
10 // should use printhex instead of printuint (decimal).
13 func bytes(s
string) (ret
[]byte) {
14 rp
:= (*slice
)(unsafe
.Pointer(&ret
))
15 sp
:= stringStructOf(&s
)
24 // The compiler emits calls to printlock and printunlock around
25 // the multiple calls that implement a single Go print or println
26 // statement. Some of the print helpers (printsp, for example)
27 // call print recursively. There is also the problem of a crash
28 // happening during the print routines and needing to acquire
29 // the print lock to print information about the crash.
30 // For both these reasons, let a thread acquire the printlock 'recursively'.
34 mp
.locks
++ // do not reschedule between printlock++ and lock(&debuglock).
36 if mp
.printlock
== 1 {
39 mp
.locks
-- // now we know debuglock is held and holding up mp.locks for us.
45 if mp
.printlock
== 0 {
50 // write to goroutine-local buffer if diverting output,
51 // or else standard error.
52 func gwrite(b
[]byte) {
57 if gp
== nil || gp
.writebuf
== nil {
62 n
:= copy(gp
.writebuf
[len(gp
.writebuf
):cap(gp
.writebuf
)], b
)
63 gp
.writebuf
= gp
.writebuf
[:len(gp
.writebuf
)+n
]
74 func printbool(v
bool) {
82 func printfloat(v
float64) {
87 case v
+v
== v
&& v
> 0:
90 case v
+v
== v
&& v
< 0:
95 const n
= 7 // digits printed
121 for i
:= 0; i
< n
; i
++ {
131 // format +d.dddd+edd
132 for i
:= 0; i
< n
; i
++ {
134 buf
[i
+2] = byte(s
+ '0')
148 buf
[n
+4] = byte(e
/100) + '0'
149 buf
[n
+5] = byte(e
/10)%10
+ '0'
150 buf
[n
+6] = byte(e%10
) + '0'
154 func printcomplex(c complex128
) {
155 print("(", real(c
), imag(c
), "i)")
158 func printuint(v
uint64) {
161 for i
--; i
> 0; i
-- {
162 buf
[i
] = byte(v%10
+ '0')
171 func printint(v
int64) {
179 func printhex(v
uint64) {
180 const dig
= "0123456789abcdef"
183 for i
--; i
> 0; i
-- {
197 func printpointer(p unsafe
.Pointer
) {
198 printhex(uint64(uintptr(p
)))
201 func printstring(s
string) {
202 if uintptr(len(s
)) > maxstring
{
203 gwrite(bytes("[string too long]"))
209 func printslice(s
[]byte) {
210 sp
:= (*slice
)(unsafe
.Pointer(&s
))
211 print("[", len(s
), "/", cap(s
), "]")
212 printpointer(sp
.array
)
215 func printeface(e eface
) {
216 print("(", e
._type
, ",", e
.data
, ")")
219 func printiface(i iface
) {
220 print("(", i
.tab
, ",", i
.data
, ")")