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.9k Golang : Pat multiplexer routing example
+13.6k Golang : Find commonalities in two slices or arrays example
+5.4k Grep : How to grep for strings inside binary data
+12.1k Golang : How to get a user home directory path?
+7.2k Golang : Check if integer is power of four example
+5k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+4k MariaDB/MySQL : Form select statement or search query with Chinese characters
+14.4k Golang : How to convert(cast) IP address to string?
+9.1k Golang : Random Rune generator
+15.2k Golang : Check if a string contains multiple sub-strings in []string?
+5.9k How to let Facebook Login button redirect to a particular URL ?
+3.9k Nginx and PageSpeed build from source CentOS example