Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:38:55+00:00 2026-06-15T17:38:55+00:00

So I am trying to make a program in GO to take a text

  • 0

So I am trying to make a program in GO to take a text file full of code and convert that into GO code and then save that file into a GO file or text file. I have been trying to figure out how to save the changes I made to the text file, but the only way I can see the changes is through a println statement because I am using strings.replace to search the string array that the text file is stored in and change each occurrence of a word that needs to be changed (ex. BEGIN -> { and END -> }). So is there any other way of searching and replacing in GO I don’t know about or is there a way to edit a text file that I don’t know about or is this impossible?

Thanks

Here is the code I have so far.

package main

import (
    "os"
    "bufio"
    "bytes"
    "io"
    "fmt"
    "strings"
)


func readLines(path string) (lines []string, errr error) {
    var (
        file *os.File
        part []byte
        prefix bool
    )
    if file, errr = os.Open(path); errr != nil {
        return
    }
    defer file.Close()

    reader := bufio.NewReader(file)
    buffer := bytes.NewBuffer(make([]byte, 0))
    for {
        if part, prefix, errr = reader.ReadLine(); errr != nil {
            break
        }
    buffer.Write(part)
        if !prefix {
            lines = append(lines, buffer.String())
            buffer.Reset()
        }
    }
    if errr == io.EOF {
        errr = nil
    }
    return
}

func writeLines(lines []string, path string) (errr error) {
    var (
        file *os.File
    )

    if file, errr = os.Create(path); errr != nil {
        return
    }
    defer file.Close()


    for _,item := range lines {

        _, errr := file.WriteString(strings.TrimSpace(item) + "\n");

        if errr != nil {

            fmt.Println(errr)
            break
        }
    }

    return
}

func FixBegin(lines []string) (errr error) {
    var(
    a string

    )
for i := 0; ; i++ {
        a = lines[i];

        fmt.Println(strings.Replace(a, "BEGIN", "{", -1))
    }

    return
}

func FixEnd(lines []string) (errr error) {
    var(
    a string

    )
for i := 0; ; i++ {
        a = lines[i];

        fmt.Println(strings.Replace(a, "END", "}", -1))
    }
    return
}

func main() {
    lines, errr := readLines("foo.txt")
    if errr != nil {
        fmt.Println("Error: %s\n", errr)
        return
    }
    for _, line := range lines {
        fmt.Println(line)
    }


    errr = FixBegin(lines)

    errr = writeLines(lines, "beer2.txt")
    fmt.Println(errr)

    errr = FixEnd(lines)
    lines, errr = readLines("beer2.txt")
    if errr != nil {
        fmt.Println("Error: %s\n", errr)
        return
    }
    errr = writeLines(lines, "beer2.txt")
    fmt.Println(errr)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:38:56+00:00Added an answer on June 15, 2026 at 5:38 pm
    jnml@fsc-r630:~/src/tmp/SO/13789882$ ls
    foo.txt  main.go
    jnml@fsc-r630:~/src/tmp/SO/13789882$ cat main.go 
    package main
    
    import (
            "bytes"
            "io/ioutil"
            "log"
    )
    
    func main() {
            src, err := ioutil.ReadFile("foo.txt")
            if err != nil {
                    log.Fatal(err)
            }
    
            src = bytes.Replace(src, []byte("BEGIN"), []byte("{"), -1)
            src = bytes.Replace(src, []byte("END"), []byte("}"), -1)
            if err = ioutil.WriteFile("beer2.txt", src, 0666); err != nil {
                    log.Fatal(err)
            }
    }
    jnml@fsc-r630:~/src/tmp/SO/13789882$ cat foo.txt 
    BEGIN
      FILE F(KIND=REMOTE);
      EBCDIC ARRAY E[0:11];
      REPLACE E BY "HELLO WORLD!";
      WRITE(F, *, E);
    END.
    jnml@fsc-r630:~/src/tmp/SO/13789882$ go run main.go 
    jnml@fsc-r630:~/src/tmp/SO/13789882$ cat beer2.txt 
    {
      FILE F(KIND=REMOTE);
      EBCDIC ARRAY E[0:11];
      REPLACE E BY "HELLO WORLD!";
      WRITE(F, *, E);
    }.
    jnml@fsc-r630:~/src/tmp/SO/13789882$ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a program that can take an .mp3 file from
I'm trying to write a program that will take an HTML file and make
I am trying to write a program that will take a text file with
Context: I'm trying to do is to make a program which would take text
Basically this is a program which I take a text file with some code
I have been trying to make a decision which approach to take to build
I am trying to make a program that will take as input a string
I am trying to make a program that takes input from a file and
I have to make a program that basically reads x lines from a file
Im trying to make a program that retrieves an endless amount of numbers that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.