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
+5.2k Golang : How to solve "too many .rsrc sections" error?
+7.3k Golang : How to check variable or object type during runtime?
+3.6k Linux/MacOSX : Search and delete files by extension
+7.3k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+8.6k Golang : Load ASN1 encoded DSA public key PEM file example
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+10.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+25.7k Golang : Find files by name - cross platform example
+3.7k MariaDB/MySQL : How to get version information
+9.5k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+5k Facebook : How to force facebook to scrape latest URL link data?