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
+8.3k Linux/Unix : fatal: the Postfix mail system is already running
+17k Golang : Check if IP address is version 4 or 6
+9.7k Golang : Translate language with language package example
+5.5k Javascript : How to replace HTML inside <div>?
+6.2k Golang : Calculate diameter, circumference, area, sphere surface and volume
+6.4k Golang : How to validate ISBN?
+19.1k Golang : Fix cannot download, $GOPATH not set error
+5.1k Golang : Reclaim memory occupied by make() example
+4.6k Javascript : How to get width and height of a div?
+28.9k Golang : Record voice(audio) from microphone to .WAV file
+36.2k Golang : Display float in 2 decimal points and rounding up or down
+19.7k Swift : Convert (cast) Int to int32 or Uint32