Puzzle 11 Explanation: Go Log
Understand the concept of log in Go.
We'll cover the following...
We'll cover the following...
Try it yourself
Try executing the code below to see the result.
Go (1.16.5)
package mainimport ("fmt""time")// Log is a log messagetype Log struct {Message stringtime.Time}func main() {ts := time.Date(2009, 11, 10, 0, 0, 0, 0, time.UTC)log := Log{"Hello", ts}fmt.Printf("%v\n", log)}
Explanation
The %v
verb will print all the struct fields.
Go (1.16.5)
package mainimport ("fmt")// Point is a 2D pointtype Point struct {X intY int}func main() {p := Point{1, 2}fmt.Printf("%v\n", p) // {1 2}}
We’d expect the teaser code to print{Hello 2009-11-10 00:00:00 +0000 UTC}
.
However, it doesn’t, due to the way the Log
struct is defined. In line 11 there is a field with no name, just a type. This is called ...