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
+6.3k PHP : Get client IP address
+10.5k Golang : cannot assign type int to value (type uint8) in range error
+19k Golang : How to make function callback or pass value from function as parameter?
+7.7k Golang : Convert(cast) io.Reader type to string
+7k Golang : Fibonacci number generator examples
+30.4k Golang : How to redirect to new page with net/http?
+15.7k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+10.4k Golang : Embed secret text string into binary(executable) file
+29.6k Golang : Login(Authenticate) with Facebook example
+17.5k Golang : Check if IP address is version 4 or 6
+4.7k Javascript : Detect when console is activated and do something about it
+15.7k Golang : How to convert(cast) IP address to string?