Home > OS >  Using function from `main` package into main.go file
Using function from `main` package into main.go file

Time:01-12

I have 2 files into the root directory: main.go and utils.go.

main.go file is:

package main

func main() {
   CustomPrint()
}

utils.go file:

package main

import "fmt"

func CustomPrint() {
   fmt.Println("example")
}

However, I get an error because customPrint is not recognized.

Is there any way to do that without create another package to store utils.go file?

CodePudding user response:

yes. just build it with * wildcard. like this: go build *.go

CodePudding user response:

You can't have more than one main in your package. This can be implemented like:

main.go

package main

import (
    util "./utils"
)

func main() {
    util.CustomPrint()
}

/utils/utils.go

package utils

import "fmt"

func CustomPrint() {
    fmt.Println("example")
}
  •  Tags:  
  • Related