Last active
February 1, 2022 11:59
-
-
Save IAFahim/84bb9f199ba44d718a99cf0f57400cfb to your computer and use it in GitHub Desktop.
Golang problem solving template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func (in *${NAME}) run() { | |
#[[$END$]]# | |
} | |
func main() { | |
file, err := os.Open("input.txt") | |
var F *bufio.Reader | |
if err == nil { | |
F = bufio.NewReader(file) | |
os.Stdin = file | |
} else { | |
F = bufio.NewReader(os.Stdin) | |
} | |
defer func(file *os.File) { | |
_ = file.Close() | |
}(file) | |
New${NAME}(F).run() | |
} | |
func New${NAME}(r *bufio.Reader) *${NAME} { | |
return &${NAME}{ | |
sc: r, | |
split: []string{}, | |
index: 0, | |
separator: " ", | |
} | |
} | |
type ${NAME} struct { | |
sc *bufio.Reader | |
split []string | |
index int | |
separator string | |
} | |
func (in *${NAME}) GetLine() string { | |
line, err := in.sc.ReadString('\n') | |
if err != nil { | |
fmt.Println("error line:", line, " err: ", err) | |
} | |
in.split = []string{} | |
in.index = 0 | |
return line | |
} | |
func (in *${NAME}) load() { | |
if len(in.split) <= in.index { | |
in.split = strings.Split(in.GetLine(), in.separator) | |
in.index = 0 | |
} | |
} | |
func (in *${NAME}) nextInt() int { | |
in.load() | |
val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index])) | |
in.index++ | |
return val | |
} | |
func (in *${NAME}) nextInt64() int64 { | |
in.load() | |
val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64) | |
in.index++ | |
return val | |
} | |
func (in *${NAME}) nextString() string { | |
in.load() | |
val := strings.TrimSpace(in.split[in.index]) | |
in.index++ | |
return val | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment