Skip to content

Instantly share code, notes, and snippets.

@markuswustenberg
Created May 24, 2017 11:57
Show Gist options
  • Save markuswustenberg/d912486c25989ccd325e043aa9e2c612 to your computer and use it in GitHub Desktop.
Save markuswustenberg/d912486c25989ccd325e043aa9e2c612 to your computer and use it in GitHub Desktop.
Malloc program in Go
package main
import (
"fmt"
"strconv"
)
func main() {
var mib int
var answer string
for {
fmt.Println("How much memory, in MiB?")
fmt.Scanln(&answer)
var err error
mib, err = strconv.Atoi(answer)
if err != nil {
fmt.Println("Not a number, try again.")
continue
}
break
}
b := mib * 1024 * 1024
arr := make([]byte, b)
for i := range arr {
arr[i] = 1
}
ignore(arr)
fmt.Printf("Allocated %v bytes.\n", b)
fmt.Println("Press enter to exit.")
fmt.Scanln(&answer)
}
func ignore(_ []byte) {
// Nothing
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment