-1

this is my file tree:

.
├── float.go
└── main.go

main.go:

package main

func main(){
    Float()
}

float.go:

package main

import "fmt"

func Float(){
    fmt.Println( "hello world")
}

when I try to compile the main.go, it throws an error

command-line-arguments ./main.go:4:2: undefined: Float

Why can't I use the function defined in another file of same packages?

Himanshu
  • 12,071
  • 7
  • 46
  • 61
Shuai Li
  • 2,426
  • 4
  • 24
  • 43
  • 1
    If you break your `main` package into multiple files, you have to list all when running it: `go run main.go float.go` – icza Jul 27 '18 at 11:13
  • as @icza has mentioned try running `go run *.go` when you have multiple files. – vishnu narayanan Jul 27 '18 at 11:15
  • 1
    [`go run` is a toy](https://github.com/golang/go/issues/13440). As soon as you have something non-trivial (and a package with more than one source file qualifies) you should switch to `go build` or ` go install`. – Peter Jul 27 '18 at 12:16

1 Answers1

0

As package main is split into multiple files, you have to pass all the files to the go compiler when building.

Either you could list it one by one like

go run main.go float.go 

or you could

go run *.go
vishnu narayanan
  • 3,813
  • 2
  • 24
  • 28