Skip to content

Instantly share code, notes, and snippets.

@AkashBabu
Created November 5, 2018 09:46
Show Gist options
  • Save AkashBabu/938909700e5604483e2b44815a3e2c11 to your computer and use it in GitHub Desktop.
Save AkashBabu/938909700e5604483e2b44815a3e2c11 to your computer and use it in GitHub Desktop.
Fast IO for scanning input via STDIn for competitive programming in Golang
/**
* This snippet assumes large input and large output and that all the input contains numbers in the form of string
*/
package main
import (
"bufio"
"io/ioutil"
"os"
"strconv"
)
var bytes []byte
var l, max int
/**
* Perform Quick Scan of 1 data(with no spaces in-between) on the buffered input
*/
func fastScan() int {
b := bytes[l]
// Check if the bytes is not a number
for b < 48 || b > 57 {
l++
b = bytes[l]
}
result := 0
for 47 < b && b < 58 {
result = (result << 3) + (result << 1) + int(b-48)
l++
if l > max {
return result
}
b = bytes[l]
}
return result
}
func main() {
// Read all the input into Buffer
bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
max = len(bytes) - 1
// Create a write buffer directed to STDOUT
ws := bufio.NewWriter(os.Stdout)
// Read the first param
t := fastScan()
for t > 0 {
t--
ws.WriteString(strconv.Itoa(t) + "\n")
}
ws.Flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment