Skip to content

Commit 76dbf78

Browse files
flyinprogrammerjcbwlkr
authored andcommitted
Println cleanup (ardanlabs#247)
* address println builtin usage * don't confuse function with builtin * code should use cwd path * should have newline because we're iterating * fix package name * simpler speak print formatting * nitpick: use tab characters
1 parent 109f038 commit 76dbf78

File tree

10 files changed

+50
-39
lines changed

10 files changed

+50
-39
lines changed

topics/go/concurrency/channels/advanced/example1/example1.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// https://golang.org/ref/mem#tmp_7
77
package main
88

9+
import "fmt"
10+
911
func main() {
1012
unBuffered()
1113
buffered()
@@ -27,7 +29,7 @@ func unBuffered() {
2729
c <- 0
2830

2931
// We are guaranteed to print "hello, world".
30-
println(a)
32+
fmt.Println(a)
3133
}
3234

3335
// With buffered channels, the send happens before the corresponding receive.
@@ -45,7 +47,7 @@ func buffered() {
4547
<-c
4648

4749
// We are guaranteed to print "hello, world".
48-
println(a)
50+
fmt.Println(a)
4951
}
5052

5153
// With both types of channels, a close happens before the corresponding receive.
@@ -63,5 +65,5 @@ func closed() {
6365
<-c
6466

6567
// We are guaranteed to print "hello, world".
66-
println(a)
68+
fmt.Println(a)
6769
}

topics/go/design/composition/grouping/example1/example1.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ type Animal struct {
1919
// Speak provides generic behavior for all animals and
2020
// how they speak.
2121
func (a *Animal) Speak() {
22-
fmt.Println("UGH!",
23-
"My name is", a.Name,
24-
", it is", a.IsMammal,
25-
"I am a mammal")
22+
fmt.Printf(
23+
"UGH! My name is %s, it is %t I am a mammal\n",
24+
a.Name,
25+
a.IsMammal,
26+
)
2627
}
2728

2829
// Dog contains everything an Animal is but specific
@@ -34,10 +35,12 @@ type Dog struct {
3435

3536
// Speak knows how to speak like a dog.
3637
func (d *Dog) Speak() {
37-
fmt.Println("Woof!",
38-
"My name is", d.Name,
39-
", it is", d.IsMammal,
40-
"I am a mammal with a pack factor of", d.PackFactor)
38+
fmt.Printf(
39+
"Woof! My name is %s, it is %t I am a mammal with a pack factor of %d.\n",
40+
d.Name,
41+
d.IsMammal,
42+
d.PackFactor,
43+
)
4144
}
4245

4346
// Cat contains everything an Animal is but specific
@@ -49,10 +52,12 @@ type Cat struct {
4952

5053
// Speak knows how to speak like a cat.
5154
func (c *Cat) Speak() {
52-
fmt.Println("Meow!",
53-
"My name is", c.Name,
54-
", it is", c.IsMammal,
55-
"I am a mammal with a climb factor of", c.ClimbFactor)
55+
fmt.Printf(
56+
"Meow! My name is %s, it is %t I am a mammal with a climb factor of %d.\n",
57+
c.Name,
58+
c.IsMammal,
59+
c.ClimbFactor,
60+
)
5661
}
5762

5863
func main() {

topics/go/design/composition/grouping/example2/example2.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ type Dog struct {
2727
// This makes a Dog now part of a group of concrete
2828
// types that know how to speak.
2929
func (d *Dog) Speak() {
30-
fmt.Println("Woof!",
31-
"My name is", d.Name,
32-
", it is", d.IsMammal,
33-
"I am a mammal with a pack factor of", d.PackFactor)
30+
fmt.Printf(
31+
"Woof! My name is %s, it is %t I am a mammal with a pack factor of %d.\n",
32+
d.Name,
33+
d.IsMammal,
34+
d.PackFactor,
35+
)
3436
}
3537

3638
// Cat contains everything a Cat needs.
@@ -44,10 +46,12 @@ type Cat struct {
4446
// This makes a Cat now part of a group of concrete
4547
// types that know how to speak.
4648
func (c *Cat) Speak() {
47-
fmt.Println("Meow!",
48-
"My name is", c.Name,
49-
", it is", c.IsMammal,
50-
"I am a mammal with a climb factor of", c.ClimbFactor)
49+
fmt.Printf(
50+
"Meow! My name is %s, it is %t I am a mammal with a climb factor of %d.\n",
51+
c.Name,
52+
c.IsMammal,
53+
c.ClimbFactor,
54+
)
5155
}
5256

5357
func main() {

topics/go/design/error_handling/example4/example4.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// All material is licensed under the Apache License Version 2.0, January 2004
22
// http://www.apache.org/licenses/LICENSE-2.0
33

4-
// Package example5 provides code to show how to implement behavior as context.
5-
package example5
4+
// Package example4 provides code to show how to implement behavior as context.
5+
package example4
66

77
import (
88
"bufio"

topics/go/exercises/chatbot/exercise/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ func main() {
1919

2020
// Print something to welcome users and tell them what they can ask about.
2121
fmt.Println(`Welcome to my chatbot! Exit with ctrl-c or type "exit" or "quit".`)
22-
fmt.Println(`You can ask me about:`)
23-
fmt.Println(` The weather.`)
24-
fmt.Println(` How I'm feeling.`)
25-
fmt.Println(` The game last night.`)
22+
fmt.Println("You can ask me about:")
23+
fmt.Println("\tThe weather.")
24+
fmt.Println("\tHow I'm feeling.")
25+
fmt.Println("\tThe game last night.")
2626

2727
// Print a > to prompt the user to type something. Don't use Println for this.
2828
fmt.Print("> ")

topics/go/language/embedding/exercises/exercise1/exercise1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func main() {
5858
// Range of each haystack value and check the term.
5959
for _, hs := range haystack {
6060
if hs.find(needle) {
61-
fmt.Printf("FOUND: %+v", hs)
61+
fmt.Printf("FOUND: %+v\n", hs)
6262
}
6363
}
6464
}

topics/go/packages/encoding/example2/example2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type (
3838
func main() {
3939

4040
// Open the file.
41-
file, err := os.Open("./topics/go/packages/encoding/example2/data.json")
41+
file, err := os.Open("data.json")
4242
if err != nil {
4343
fmt.Println("Open File", err)
4444
return

topics/go/packages/encoding/exercises/exercise1/exercise1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type user struct {
2323
func main() {
2424

2525
// Open the file.
26-
file, err := os.Open("./topics/go/packages/encoding/exercises/exercise1/data.json")
26+
file, err := os.Open("data.json")
2727
if err != nil {
2828
fmt.Println("Open File", err)
2929
return

topics/go/packages/io/advanced/example1/example1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
func main() {
2222

2323
// Open the file for reading.
24-
file, err := os.Open("./topics/go/packages/io/advanced/example1/data.json")
24+
file, err := os.Open("data.json")
2525
if err != nil {
2626
fmt.Println("Open File", err)
2727
return

topics/web/serializers/example4/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func main() {
2626
log.Fatal(err)
2727
}
2828

29-
print("Zero value User", b)
30-
print("Note 'roles' is null", nil)
29+
printMsgData("Zero value User", b)
30+
printMsgData("Note 'roles' is null", nil)
3131

3232
// Initialize roles for an otherwise zeroed User
3333
u := User{
@@ -39,8 +39,8 @@ func main() {
3939
log.Fatal(err)
4040
}
4141

42-
print("User with empty roles slice", b)
43-
print("Note 'roles' is [] not null", nil)
42+
printMsgData("User with empty roles slice", b)
43+
printMsgData("Note 'roles' is [] not null", nil)
4444

4545
// Fill in data for user
4646
u = User{
@@ -54,11 +54,11 @@ func main() {
5454
log.Fatal(err)
5555
}
5656

57-
print("User with data", b)
57+
printMsgData("User with data", b)
5858

5959
fmt.Println("\nNote in all examples 'email' is missing.")
6060
}
6161

62-
func print(msg string, data []byte) {
62+
func printMsgData(msg string, data []byte) {
6363
fmt.Printf("%30s | %s\n", msg, string(data))
6464
}

0 commit comments

Comments
 (0)