Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 840 Bytes

golang_find_replace.md

File metadata and controls

39 lines (29 loc) · 840 Bytes

Golang find & replace from a file

package main

import (
        "io/ioutil"
        "log"
        "strings"
)

func main() {
        input, err := ioutil.ReadFile("myfile")
        if err != nil {
                log.Fatalln(err)
        }

        lines := strings.Split(string(input), "\n")

        for i, line := range lines {
                if strings.Contains(line, "]") {
                        lines[i] = "LOL"
                }
        }
        output := strings.Join(lines, "\n")
        err = ioutil.WriteFile("myfile", []byte(output), 0644)
        if err != nil {
                log.Fatalln(err)
        }
}

Reference