Question

First function

ReadF2C

takes a filename and channel, reads from file and inputs in channel. Second function

WriteC2F

takes 2 channels and filename, takes value of each channel and saves the lower value in the output file. I'm sure there is a few syntax errors but i'm new to GO

package main

import (
    "fmt"
    "bufio"
    "os"
    "strconv"
)

func main() {
    fmt.Println("Hello World!\n\n")
    cs1 := make (chan int)
    var nameinput string = "input.txt"
    readF2C(nameinput,cs1)
    cs2 := make (chan int)
    cs3 := make (chan int)
    cs2 <- 10
    cs2 <- 16
    cs2 <- 7
    cs2 <- 2
    cs2 <- 5
    cs3 <- 8
    cs3 <- 15
    cs3 <- 14
    cs3 <- 1
    cs3 <- 6
    var nameoutput string = "output.txt"
    writeC2F (nameoutput,cs2,cs3)
}

func readF2C (fn string, ch chan int){
    f,err := os.Open(fn)
    r := bufio.NewReader(f)
    for err != nil { // not end of file 

    fmt.Println(r.ReadString('\n'))
    ch <- r.ReadString('\n')
    }

    if err != nil {
        fmt.Println(r.ReadString('\n'))
            ch <- -1
    }

}
func writeC2F(fn string, // output text file
ch1 chan int, // first input channel
ch2 chan int){
    var j int = 0
    var channel1temp int
    var channel2temp int
        f,_ := os.Create(fn)
        w := bufio.NewWriter(f)
    channel1temp = <-ch1
    channel2temp = <-ch2
    for j := 1; j <= 5; j++ {
     if (channel2temp < channel1temp){

        n4, err := w.WriteString(strconv.Itoa(channel1temp))
        } else{

        n4, err := w.WriteString(strconv.Itoa(channel2temp))
        }
    w.flush()
    }
}

This is the error messages I get:

prog.go:38: multiple-value r.ReadString() in single-value context
prog.go:65: w.flush undefined (cannot refer to unexported field or method bufio.(*Writer)."".flush)
Was it helpful?

Solution

There are multiple errors:

1)

Unlike C, Go enforces you to have your curly braces directly after your statements. So for an if case (and the same for func), instead of doing it like this:

if (channel2temp < channel1temp)
{

use this

if channel2temp < channel1temp {

2)

There is no while in Go. Use for

for {
...
}

or

for channel1temp != null || channel2temp != null {
...
}

3)

Usage of non-declared variables. Often easy to fix by making a short variable declaration the first time you initialize the variable. So instead of:

r = bufio.NewReader(file)

use

r := bufio.NewReader(file)

4)

Trying to a assign multi-value return into a single variable. If a function returns two values and you only need one, the variable you don't want can be discarded by assigning it to _. So instead of:

file := os.Open(fn)

use

file, _ := os.Open(fn)

but best practice would be to catch that error:

file, err := os.Open(fn)
if err != nil {
    panic(err)
}

There are more errors on top of this, but maybe it will get you started. I also suggest reading Effective Go since it will explain many of the things I've just mentioned.

Edit:

And there are help online for sure. It might be a new language, but the online material is really useful. Below is a few that I used when learning Go:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top