Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
In Unix/Linux system administration, there are times when the system administrator needs to reduce the size of a file to 0 immediately. Such situation usually involves log files that had taken up all the available disk space and caused other running applications that need disk space to behave oddly.
This is usually done by pointing the Unix/Linux's "black hole" (/dev/null) to the offending file. Such as:
$cat /dev/null > somefile.log
or sending an application logs to /dev/null
In Golang, the ioutil.Discard
variable is similar to /dev/null
and we can use it to send unwanted log output or basically any unwanted data.
package main
import (
"io/ioutil"
"log"
"os"
)
func main() {
log.Println("You can see this log message")
// direct all log messages to /dev/null a.k.a Blackhole
log.SetOutput(ioutil.Discard)
log.Println("But not this log message!")
}
References:
https://www.socketloop.com/tutorials/golang-how-to-save-log-messages-to-file
See also : Golang : Secure file deletion with wipe example
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.4k Golang : Read integer from file into array
+23.5k Golang : Fix type interface{} has no field or no methods and type assertions example
+12.9k Golang : Handle or parse date string with Z suffix(RFC3339) example
+8k Golang : Add build version and other information in executables
+11.9k Golang : md5 hash of a string
+18.9k Golang : Check if directory exist and create if does not exist
+4.9k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+4.5k Chrome : How to block socketloop.com links in Google SERP?
+4.7k Javascript : How to get width and height of a div?
+21.9k Golang : Match strings by wildcard patterns with filepath.Match() function
+7.6k Golang : Scan files for certain pattern and rename part of the files
+6.8k Golang : Levenshtein distance example