Skip to content

Instantly share code, notes, and snippets.

@vladimirvivien
Created September 20, 2017 12:41
Show Gist options
  • Save vladimirvivien/5210dae05fce3068f02eda5b01ba073a to your computer and use it in GitHub Desktop.
Save vladimirvivien/5210dae05fce3068f02eda5b01ba073a to your computer and use it in GitHub Desktop.
func main() {
reader := strings.NewReader("Clear is better than clever")
p := make([]byte, 4)
for {
n, err := reader.Read(p)
if err != nil{
if err == io.EOF {
fmt.Println(string(p[:n])) //should handle any remainding bytes.
break
}
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(p[:n]))
}
}
@fulldump
Copy link

Would you consider reordering to improve clarity?

	for {
		n, err := reader.Read(p)
		if err == io.EOF {
			fmt.Println(string(p[:n])) //should handle any remainding bytes.
			break
		}
		if err != nil{
		    fmt.Println(err)
		    os.Exit(1)
		}
		fmt.Println(string(p[:n]))
	}

@isopropylcyanide
Copy link

+1 to reordering. Having nested conditionals that increase the cognitive complexity is not idiomatic go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment