-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
45 lines (36 loc) · 1018 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
fmt.Println("Simple Text Editor in Go")
//wherever
filename := "example.txt" // This can be taken from command-line arguments
// Reading from a file
content, err := os.ReadFile(filename)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
fmt.Println("Current Content:", string(content))
// Simple text input
reader := bufio.NewReader(os.Stdin)
fmt.Println("Enter text to append:")
input, _ := reader.ReadString('\n')
input = strings.TrimSuffix(input, "\n")
// Appending to the file
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer f.Close()
if _, err = f.WriteString(input + "\n"); err != nil {
fmt.Println("Error writing to file:", err)
return
}
fmt.Println("Updated file successfully.")
}