Import / embed other files - go (golang)

0

I'm studying go (golang) and I have a question about importing files, the doubt is about something "elemental" but I could not find anything specifically about it.

I have a package ( test ) arranged like this:

test -> main.go
     -> test.go

Would you like to import test.go into main.go ?

//em javascript seria algo assim:
//main.go
require('./test.go')

How to do the same on go?

    
asked by anonymous 30.10.2017 / 03:36

2 answers

1

Golang uses a package system, so there's no way you can import "a single file".

I do not know what your situation is exactly there, but if both files belong to the same package, then there's no need to import anything, it's like they're a single large file. If they are in separate packages, you can import the package.

import "meupacote"

or

import (
    "umpacote"
    "outropacote"
)

Remembering that the import declaration should come soon after the name of your package.

package main

import "fmt"

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

Once imported, all exported identifiers (first capital letter) of the package are available for use.

I recommend using the word test with care, after all, it has special meaning when compiling and running tests.

    
30.10.2017 / 21:37
0

As it is inside the main directory, it is not necessary to import, in this case just call the functions and variables that are inside the test.go file

If your file was between or package like:

- > main - > main.go              - > example / - > example.go

In this case you would have to import into your main.go file:

package main
import "principal/exemplo"
import "fmt"

func main(){
   fmt.Println(exemplo("Olá, Exemplo!"))
}
    
03.11.2017 / 23:33