[highly opinionated] Template for Go on CodeForces.com

Правка en4, от eval_apply, 2021-11-16 01:02:50

What?

A small, lean and efficient template for writing code on CodeForces.com using Go. Mostly consists of input/output improvements and helper functions.

Why?

Because I 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

Unroll me!

Suggestions?

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

Happy solving! :)

Теги template, go template, go, golang

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en7 Английский 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 Английский eval_apply 2021-11-16 01:03:48 0 (published)
en5 Английский eval_apply 2021-11-16 01:03:22 6
en4 Английский eval_apply 2021-11-16 01:02:50 82
en3 Английский eval_apply 2021-11-16 01:00:34 61
en2 Английский eval_apply 2021-11-16 00:58:22 4482
en1 Английский eval_apply 2021-11-16 00:50:43 1396 Initial revision (saved to drafts)