What matters here is not so much what you do in this cycle. It does not look like you will directly edit the file on the fly.
The easiest solution for you is to simply replace the string in the array and then write the contents of the array back to your file when you are done.
Here is the code I put together in a minute or two. It compiles correctly and runs on my machine.
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) } }
There is also (with the same code) https://gist.github.com/dallarosa/b58b0e3425761e0a7cf6
source share