9951 explained code solutions for 126 technologies

golangHow to overwrite content in file if it exists

package main

import (

func main() {
	f, _ := os.OpenFile("/tmp/go.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  defer f.Close()
  f.WriteString("new content\n")
}ctrl + c
package main

default package declaration


include operating-system level library

func main() {

declare main function that will be launched automatically


opens file handler


path to file to open


permissions used to append to file


file permissions to set

defer f.Close()

deferred file handler close


writes string to a given file