[highly opinionated] Template for Go on CodeForces.com

Revision en2, by eval_apply, 2021-11-16 00:58:22

What?

A quick and efficient template for writing code on CodeForces.com using Go. Mostly consists of input/output improvers and helpers.

Why?

Because I've needed my code to be more compact, wanted more clarity and ease of use. And to speed-up a bit too.

And now I share this template to you guys, to make Go a bit more appealing to use in competitive programming.

P.S.: Mike even updated Go compiler to the current latest version (1.17.3), so using Go can be quite worth a candle :)

How you've made it?

I've got rid of the rather slow group of simple scanners from "fmt", namely:

  • fmt.Scan
  • fmt.Scanln
  • fmt.Scanf

because of their clumsiness and their slowness compared to other available options in Go. To learn more, start with checking some benchmarks from this post.

Instead, there is a reader from bufio, which reads from os.Stdin and is much faster. And it simplifies the code of the program too, because instead of:

var variable type
fmt.Scan(variable)

You can now write like:

variable := readType()

To read an Array (or Slice, actually) with items of some type, located on the current line and separated by spaces, just append 's' at the end of the desired read function like so:

arrayOfVars := readTypes()

Template itself

/*
	A template for Go on codeforces.com
	Author: Yakov, frvr.ru
*/

package main

import (
	"bufio"
	"fmt"
	"os"
	"reflect"
	"strconv"
	"strings"
)

func main() {
	// t := readInt()
	// writeLine(typeOf(t))
}

/* ~~~~~~~~~~ */
/* ~~~~~~~~~~ */
/* ~~~~~~~~~~ */

var in = bufio.NewReader(os.Stdin)

type float = float32
type double = float64
type long = int64
type ulong = uint64

func typeOf(arg interface{}) string {
	switch reflect.TypeOf(arg).Kind() {
	case reflect.Int:
		return "int"
	case reflect.Int8:
		return "int8"
	case reflect.Int16:
		return "int16"
	case reflect.Int32:
		return "int32"
	case reflect.Int64:
		return "int64"
	case reflect.Uint:
		return "uint"
	case reflect.Uint8:
		return "uint8"
	case reflect.Uint16:
		return "uint16"
	case reflect.Uint32:
		return "uint32"
	case reflect.Uint64:
		return "uint64"
	case reflect.Float32:
		return "float32"
	case reflect.Float64:
		return "float64"
	case reflect.Complex64:
		return "complex64"
	case reflect.Complex128:
		return "complex128"
	case reflect.Bool:
		return "bool"
	case reflect.String:
		return "string"
	case reflect.Chan:
		return "chan"
	case reflect.Ptr:
		return "ptr"
	case reflect.Uintptr:
		return "uintptr"
	case reflect.UnsafePointer:
		return "unsafeptr"
	case reflect.Interface:
		return "interface"
	case reflect.Array:
		return "array"
	case reflect.Slice:
		return "slice"
	case reflect.Map:
		return "map"
	case reflect.Struct:
		return "struct"
	case reflect.Func:
		return "func"
	case reflect.Invalid:
		return "invalid"
	default:
		return "nil"
	}
}

/* ~~~~~~~~~~ */

func _scln() string {
	ln, _ := in.ReadString('\n')
	return strings.Trim(ln, " \r\n")
}
func _sc(s []string) string {
	if len(s) == 0 {
		return _scln()
	}
	return s[0]
}

func readLine() string              { return _scln() }
func readString() string            { return _scln() }
func readStrings() []string         { return strings.Split(_scln(), " ") }
func readBool(s ...string) bool     { t, _ := strconv.ParseBool(_sc(s)); return t }
func readByte(s ...string) byte     { t, _ := strconv.ParseUint(_sc(s), 10, 8); return byte(t) }
func readDouble(s ...string) double { t, _ := strconv.ParseFloat(_sc(s), 64); return t }
func readFloat(s ...string) float   { t, _ := strconv.ParseFloat(_sc(s), 32); return float32(t) }
func readInt(s ...string) int       { t, _ := strconv.Atoi(_sc(s)); return t }
func readLong(s ...string) long     { t, _ := strconv.ParseInt(_sc(s), 10, 64); return t }
func readULong(s ...string) ulong   { t, _ := strconv.ParseUint(_sc(s), 10, 64); return t }

/* ~~~~~~~~~~ */

// Too much overhead with universal method using interface{}, wait for the generics,
// but for now — don't look at the redundancy of the code after that commentary :)

func readBools() []bool {
	strs := readStrings()
	arr := make([]bool, len(strs))
	for i, s := range strs {
		arr[i] = readBool(s)
	}
	return arr
}

func readBytes() []byte {
	strs := readStrings()
	arr := make([]byte, len(strs))
	for i, s := range strs {
		arr[i] = readByte(s)
	}
	return arr
}

func readDoubles() []double {
	strs := readStrings()
	arr := make([]double, len(strs))
	for i, s := range strs {
		arr[i] = readDouble(s)
	}
	return arr
}

func readFloats() []float {
	strs := readStrings()
	arr := make([]float, len(strs))
	for i, s := range strs {
		arr[i] = readFloat(s)
	}
	return arr
}

func readInts() []int {
	strs := readStrings()
	arr := make([]int, len(strs))
	for i, s := range strs {
		arr[i] = readInt(s)
	}
	return arr
}

func readLongs() []long {
	strs := readStrings()
	arr := make([]long, len(strs))
	for i, s := range strs {
		arr[i] = readLong(s)
	}
	return arr
}

func readULongs() []ulong {
	strs := readStrings()
	arr := make([]ulong, len(strs))
	for i, s := range strs {
		arr[i] = readULong(s)
	}
	return arr
}

/* ~~~~~~~~~~ */

func write(arg ...interface{})                 { fmt.Print(arg...) }
func writeLine(arg ...interface{})             { fmt.Println(arg...) }
func writeFormat(f string, arg ...interface{}) { fmt.Printf(f, arg...) }

Suggestions?

Leave them in the comments below, I'll update the template, if necessary.

Happy solving! :)

Tags template, go template, go, golang

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English eval_apply 2023-01-09 14:40:01 22 Tiny change: 'n Author: Yakov, frvr.ru\n*/\n\npa' -> 'n Author: algo.tensai.me\n*/\n\npa'
en6 English eval_apply 2021-11-16 01:03:48 0 (published)
en5 English eval_apply 2021-11-16 01:03:22 6
en4 English eval_apply 2021-11-16 01:02:50 82
en3 English eval_apply 2021-11-16 01:00:34 61
en2 English eval_apply 2021-11-16 00:58:22 4482
en1 English eval_apply 2021-11-16 00:50:43 1396 Initial revision (saved to drafts)