4

I have two go files in a directory.

  Go  
   ├── mains.go  
   └── vars.go

The code for the mains.go and vars.go are given below:

mains.go
--------
package main

import "fmt"

func main() {
     fmt.Println("This is the mains file")
}


vars.go
--------
package main

import "fmt"

func main() {
     fmt.Println("This is the vars file")
}

While running the file individually using the terminal command

go run mains.go
go run vars.go

I am getting the output. When I am using VScode, I am getting the following error

main redeclared in this block
previous declaration at ./mains.go:5:6

Due to this error, I am not able to run the code. The code runs fine when each file is separated into folders. I tried to remove the main declaration from the file before saving but the autocomplete/autoformat feature fills the package main & import "fmt" commands automatically. My doubts are :

  • Is this the problem with the editor? (As the terminal commands run fine)

  • Any other recommended IDE for go?

My specs

Ubuntu : 16.04
Visual Studio Code : 1.23.1
go version : 1.9.2

Sooraj
  • 514
  • 4
  • 20

2 Answers2

2

It doesn't make sense to redeclare the main function twice in the same package. You mentioned that the code works fine placing the mains.go and vars.go files in separate folders. That is expected and this is what needs to be done - placing the files in different folders.

If however, you need to run both functions, change your setup using a simple goroutine as defined below:

// main.go
package main

import (
          "fmt"
          "sync"
)

var wg sync.WaitGroup

func main() {
     fmt.Println("This is the mains file")

     wg.Add(1)
     go vars()

     // do other stuff...
     wg.Wait()
}

// vars.go
package main

import "fmt"

func vars() {
     fmt.Println("This is the vars file")
     // do other stuff...

     wg.Done()
}

This would allow you to run both functions at the same time whilst still in the same package. Since the functions can't have the same function name: main(), you need to change one of them to something else as I've done.

Note however that you could simply call the vars() function without doing so concurrently.

smartmek
  • 51
  • 5
  • I am an absolute beginner in go. I am familiar with python. I thought just like writing a pycode you can open up two different files and then do the imports & rest of the code. Seems like it's not the case in the go. Need to look more into the docs. Thanks for the detailed explanation – Sooraj Jun 01 '18 at 11:47
  • 1
    You're welcome @Sooraj. Golang is very strict but you'll come to learn that it's a good thing when you get used to it. – smartmek Jun 01 '18 at 12:25
1

Have you declared two main functions in two different files in same directory? If yes, you can not as you can only have single entry point for go program.

Also Golang recommends to have single package per directory. You can have multiple file in a same package.

For example:

- Root directory of program
   -- main.go - package main (define main func here) 
   -- vars.go - package main (You can not redefine main func or package in this file, since everything under directory falls in to same package in golang)

   - lib directory
   -- lib.go - package lib
   -- something.go - package lib  

Hope that helps!

kashpatel
  • 696
  • 1
  • 6
  • 18
  • 1
    I have edited my question please take look at it. I have followed your advice but it doesn't seem to work. – Sooraj Jun 01 '18 at 05:10
  • Thanks for your reply. It seems I have to look more into the go docs. – Sooraj Jun 01 '18 at 11:47
  • 1
    No problem. You can not have function with same name in same package. Even if they are in two separate files under directory. Golang is amazing and easy to pick up. You will fall in love with it in no time :) – kashpatel Jun 01 '18 at 12:52