This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
445 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
) | ||
|
||
func main() { | ||
|
||
a, b := 10, 20 | ||
|
||
log.Print("Use Print to log.") | ||
log.Println("Ditto for Println.") | ||
log.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
|
||
// Create a file | ||
logFile, err := os.Create("log1.txt") | ||
if err != nil { | ||
panic("Could not open file") | ||
} | ||
|
||
// Close the file after main returns | ||
defer logFile.Close() | ||
|
||
a, b := 10, 20 | ||
|
||
// We will not use the other options | ||
myLog := log.New(logFile, "", 0) | ||
|
||
myLog.Print("Use Print to log.") | ||
myLog.Println("Ditto for Println.") | ||
myLog.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
|
||
// Create a file | ||
logFile, err := os.Create("log1.txt") | ||
if err != nil { | ||
panic("Could not open file") | ||
} | ||
|
||
// Close the file after main returns | ||
defer logFile.Close() | ||
|
||
// Create a second file | ||
logFile2, err := os.Create("log2.txt") | ||
if err != nil { | ||
panic("Could not open file2") | ||
} | ||
|
||
defer logFile2.Close() | ||
|
||
// Create a buffer | ||
var buflog bytes.Buffer | ||
|
||
multiW := io.MultiWriter(logFile, logFile2, &buflog, os.Stdout) | ||
|
||
a, b := 10, 20 | ||
|
||
// Log to multiW | ||
myLog := log.New(multiW, "", 0) | ||
|
||
myLog.Print("Use Print to log.") | ||
myLog.Println("Ditto for Println.") | ||
myLog.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b) | ||
|
||
// Print buffer | ||
fmt.Println("Buffer:") | ||
fmt.Println(buflog.String()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
|
||
a, b := 10, 20 | ||
|
||
// New logger will output to stdout with flags | ||
// Only log date and file | ||
myLog := log.New(os.Stdout, "", log.Ldate|log.Lshortfile) | ||
|
||
myLog.Print("Use Print to log.") | ||
myLog.Println("Ditto for Println.") | ||
myLog.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
|
||
a, b := 10, 20 | ||
|
||
// New logger will output to stdout with prefix "Log1: " and flags | ||
// Note the space in prefix | ||
myLog := log.New(os.Stdout, "Log1: ", log.Ldate|log.Lshortfile) | ||
|
||
myLog.Print("Use Print to log.") | ||
myLog.Println("Ditto for Println.") | ||
myLog.Printf("Use Printf and format strings. %d + %d = %d", a, b, a+b) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Use Print to log. | ||
Ditto for Println. | ||
Use Printf for format strings. 10 + 20 = 30 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Use Print to log. | ||
Ditto for Println. | ||
Use Printf for format strings. 10 + 20 = 30 |
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
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
Oops, something went wrong.