Unexpected Printf Behavior in Go WASM – Nothing Prints
Tags: go, languages, webassembly, formatting
Published: 2020-08-17 15:15:53

---
title: Unexpected Printf Behavior in Go WASM – Nothing Prints
published: true
date: 2020-08-10 12:55:00 UTC
tags: Golang,Languages,WASM,formatting
canonical_url: https://qvault.io/2020/08/10/unexpected-printf-behavior-in-go-wasm/
---

 ![fmt.Printf doesn't do what you think it does WASM Go](https://qvault.io/wp-content/uploads/2020/08/fmt.Printf-300x169.png)

The post [Unexpected Printf Behavior in Go WASM – Nothing Prints](https://qvault.io/2020/08/10/unexpected-printf-behavior-in-go-wasm/) appeared first on [Qvault](https://qvault.io).

While working on [Qvault’s](https://classroom.qvault.io) [Go Playground](https://classroom.qvault.io/playground/go), I came across a very strange error. The standard library’s [fmt.Printf()](https://golang.org/pkg/fmt/?m=all#Printf) function prints nothing to the console when called. _Nothing._

For those of you who are familiar with the function, when compiled to a “normal” executable `fmt.Printf` prints a formatted string to standard output. As per the official documentation, this program:

```go
package main

import (
	"fmt"
)

func main() {
	const name, age = "Kim", 22
	fmt.Printf("%s is %d years old.", name, age)
}
```

Will print:

```
Kim is 22 years old.
```

The interesting thing is that when the same exact program is compiled using Web Assembly, we get a different result. If you want to try it, copy the above program and run it [here](https://classroom.qvault.io/playground/go).

Spoiler alert: _It doesn’t print anything._

However, if you change the program slightly:

```go
package main

import (
	"fmt"
)

func main() {
	const name, age = "Kim", 22
	// add a newline character
	fmt.Printf("%s is %d years old.\n", name, age)
}
```

Then it may print the expected:

```
Kim is 22 years old.
```

thought if you run the two programs back to back, you may actually find that this is printed instead:

```
Kim is 22 years old.Kim is 22 years old.
```

## Why?

As far as I’ve been able to gather, when compiled to Web Assembly, the `fmt.Printf` function is writing to a buffer, and that buffer is not cleared until a newline character is printed to standard out. In other words, you can call `fmt.Printf` as many times as you want, but nothing is printed until a `\n` character comes through standard output.

My current working theory is that it works this way because there is no way to print to the console in a browser without appending a newline. As an example, JavaScript’s [console.log()](https://developer.mozilla.org/en-US/docs/Web/API/Console/log) _always_ appends a newline.

I’m missing all the details I would like to have about why it works this way. If any of my readers have any more information that I can include here please hit me up on Twitter!

## Thanks For Reading!

Follow us on Twitter [@q\_vault](https://twitter.com/q_vault) if you have any questions or comments

Take game-like coding courses on [Qvault Classroom](https://classroom.qvault.io/#/)

[Subscribe](https://mailchi.mp/5c7f5c281bbe/qvault-newsletter-subscribe) to our Newsletter for more educational articles

The post [Unexpected Printf Behavior in Go WASM – Nothing Prints](https://qvault.io/2020/08/10/unexpected-printf-behavior-in-go-wasm/) appeared first on [Qvault](https://qvault.io).