Call function from different Package


(Hamxa) #1

Hey,

I am trying to create a program which opens a file and read a file through a scanner and if the first line of the text has “1” in it then it should call other function from a different package.

package main

import (
“package”
“bufio”
“fmt”
“log”
“os”
)

func main() {
file, err := os.Open(“File path”)
if err != nil {
log.Fatal(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
line := scanner.Text()
if line[0] == '1' {
	_ = function1() (here I am trying to call another function) 
	log.Fatal(err)
} else {
	fmt.Println(scanner.Text())
}

if err := scanner.Err(); err != nil {
	log.Fatal(err)
}

}

Can you please tell me where am I making mistake?


(Lutz Horn) #2

The second package will have to export the function. This requires its name to start with an upper case letter like Function. Then your application will have to import the package like import package, you already do this. And finally you have to call the function as pacakge.Function().


(Hamxa) #3

I am getting error tho that my other package does not exist in the right path, my main. go package and the package are in two different folders.


(Lutz Horn) #4

Did you take the tour and learn about packages?

There is also plenty of information on the net to learn about packages. For example:


(Hamxa) #5

Hey,

I did read it, but I don’t know how to create a program which calls different functions if the line start with the specific number. For example: Call Function A if the line [0] starts with “1” or call Function B if line starts with “2”. These functions are in the different folder but in the same directory.


(Christophe Meessen) #6

You must call scanner.Scan() before scanner.Text(). That is the intended use.
Have you verified that scanner.Text() returned you the first line of the file ?

Here is your program modified to add the Scan call

package main

import (
    “package”
    “bufio”
    “fmt”
    “log”
    “os”
)

func main() {
    file, err := os.Open(“File path”)
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    if !scanner.Scan() { // <- call to Scan() return false if end of file is reached
        fmt.Println("file is empty")
        return
    }
    line := scanner.Text()
    if line[0] == '1' {
        _ = function1() (here I am trying to call another function) 
        log.Fatal(err)
    } else {
        fmt.Println(scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

Note that your code will test if the first character is ‘1’ while you said in your initial question that you want to execute a function if the first line has “1” in it. It’s not the same test.

It looks like you a considering a folder and a directory as different things, but these words identify the same thing.

The functions may be in different files in the same folder, but their package must be main so that they are in the same package as the main() function. The files of a same package must be in the same directory (=folder).

If you have multiple go files, you must use the command go run *.go to compile and run the program.