-1

I found this to be a duplicate of this question.




Hello World

Just started learning golang and tried to figure out how to structure a larger program. Not sure if packages are the split I want or if there is something else more suitable for having multiple source files in a single directory, but here's what I tried.

package main

import "fmt"
func main() {
    fmt.Printf("hello, world\n")
}

Testing running it:

~/b2/go/src/github.com/bemmu/hello bemmu$ go run hello.go
hello, world

Two file version

I wanted to try splitting it into two files.

main.go

package main

import "fmt"
import "say"

func main() {
    say.Hello()
}

say.go

package say
import "fmt"

func Hello() {
    fmt.Printf("hello, Go\n")
}

Testing running it:

~/b2/go/src/github.com/bemmu/hello_split bemmu$ go run main.go
main.go:4:8: cannot find package "say" in any of:
    /usr/local/go/src/say (from $GOROOT)
    /Users/bemmu/b2/go/src/say (from $GOPATH)

In the docs there is an example of creating a library and importing it, but in the example case it is put into a separate directory.

Community
  • 1
  • 1
Bemmu
  • 17,849
  • 16
  • 76
  • 93

1 Answers1

1

No, Just create a new directory for another package.

zzn
  • 2,376
  • 16
  • 30