Implement print() and println() in Go

This commit is contained in:
Ayke van Laethem
2018-04-20 16:32:40 +02:00
parent ff9e7a8b77
commit 45e7376f39
4 changed files with 48 additions and 43 deletions
+41
View File
@@ -0,0 +1,41 @@
package runtime
// #include <stdio.h>
import "C"
const Compiler = "tgo"
func printstring(s string) {
for i := 0; i < len(s); i++ {
C.putchar(C.int(s[i]))
}
}
func printint(n int) {
// Print integer in signed big-endian base-10 notation, for humans to
// read.
// TODO: don't recurse, but still be compact (and don't divide/mod
// more than necessary).
if n < 0 {
C.putchar('-')
n = -n
}
prevdigits := n / 10
if prevdigits != 0 {
printint(prevdigits)
}
C.putchar(C.int((n % 10) + '0'))
}
func printbyte(c uint8) {
C.putchar(C.int(c))
}
func printspace() {
C.putchar(' ')
}
func printnl() {
C.putchar('\n')
}