Golang Code

Add Line Numbers to Log Output

· 73 words · 1 minute read #debug #example #flags #line #log #logging #numbers #output

Many programming languages allow you to print the line number of when and where something has happened. This is very useful for debugging a problem when it has occurred. By default in Go this is off, but you can turn it on by setting flags within the log.

package main

import "log"

func main() {

    // Enable line numbers in logging
    log.SetFlags(log.LstdFlags | log.Lshortfile)

    // Should say: "[date] [time] loglines.go:11: Example"
    log.Println("Example")

}
Image of Author Edd Turtle

Author: Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below. Thanks for reading!