golang program in two file not run when imported another file in (main.go) [closed] - go

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
I have worked with two package named cube.go and main.go file.
in main file I have imported cube file.
but when I run the main file it not worked.please help me to fix this problem. enter image description here

Two files in a directory are in the same package, so you don't need to import cube, you code should be like this:
package main
import (
"fmt"
)
func main() {
var box Dims // Dims declared in this package in cube.go file
box.SetSize(2,4,6)
// other methods,etc
}
If you want to import cube, it's better to put cube.go file in another directory called cube or anything else and change the package name to package cube or whatever the directory name is.
And you should remove main function in cube.go file.

Related

`init()` vs declaration in Go [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm having a little trouble understanding why init() is useful in Go. It seem that generally, anything you do in init() could be done via a declaration in the go file instead.
To put a fine point on, what's the practical difference between these two files?
// useInit.go
package main
var answer int
func init() {
answer = 42
}
// useDeclaration.go
package main
var answer int = 42
If you make a package for used in other applications and don't have a main, but you have to do something (and be sure to be done) before the app is running, you can do it in the init(). And for this to run, the host app doesn't need to do anything, it doesn't even have to use any func from your package. Good example is the mysql driver (github.com/go-sql-driver/mysql). It's init() func looks like this:
func init() {
sql.Register("mysql", &MySQLDriver{})
}
And when you import it using
import _ "github.com/go-sql-driver/mysql"
The mysql driver will be registered, so the SQL package can connect to a MySQL server, however you didn't do anything, don't even use a func from this package.
In this case the import name should be an underscore to tell the compiler to not remove the package, however it looks like you don't use it for anything.

How to access imported items [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
With following project structure
D:\src\go\my-app
+ internal\
| + utils.go
+ main.go
+ go.mod
with following file contents:
internal\utils.go:
package internal
func GetText() string {
return "hello world"
}
main.go:
package main
import (
"fmt"
"example.com/my_app/internal"
)
func main() {
fmt.Println(GetText())
}
go.mod:
module example.com/my_app
go 1.17
I'm getting following compilation error when running from the directory:
D:\src\go\my-app>go build
# example.com/my_app
.\main.go:5:2: imported and not used: "example.com/my_app/internal"
.\main.go:9:14: undefined: GetText
Any idea, what could be wrong?
PS: This question is reproducible and not caused by a typo, but by a wrong assumption (in other programming languages the imported items are accessed by their name).
Yes, the problem is quite clear:
You import example.com/my_app/internal but you don't use it. If you used it, there'd be internal.<something> somewhere in your main.
GetText() does not exist: there is no GetText() function in your main package.
Solution:
Replace GetText() by internal.GetText(). Now example.com/my_app/internal is used and GetText() is found in this package.

antlr redeclared as imported package name [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I built a directory src which contains the following:
src/
antlr/
basic_levex.go
basic_parse.go
basicparser_base_visitor.go
basicparser_visitor.go
example1.go
The antlr files look correct, and built the files called BasicLexer.g4 and BasicParser.g4 .
My example1.go file looks like:
package main
// example1.go
import (
"./antlr"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
func main() {
// Setup the input
is := antlr.NewInputStream("1 + 2 * 3")
// Create the Lexer
lexer := antlr.NewBasicLexer(is)
// Read all tokens
for {
t := lexer.NextToken()
if t.GetTokenType() == antlr.TokenEOF {
break
}
fmt.Printf("%s (%q)\n",
lexer.SymbolicNames[t.GetTokenType()], t.GetText())
}
}
The error I get on compilation is
# command-line-arguments
./example1.go:8:2: antlr redeclared as imported package name
previous declaration at ./example1.go:6:2
./example1.go:16:11: undefined: "github.com/antlr/antlr4/runtime/Go/antlr".NewBasicLexer
I don't really know what is wrong. How to fix it so it doesn't happen in the future?
Let's look at both your problems one after an other
Duplicate Import
./example1.go:8:2: antlr redeclared as imported package name
previous declaration at ./example1.go:6:2
The error message is explaining pretty well what happened, including the lines:
On line 8 you try to import something with the same name that you already have imported on line 6.
Aside that, Go Does not support relative import, like Flimzy has pointed out in the comments.
To fix this remove the relative import on line 6.
Undefined function
./example1.go:16:11: undefined: "github.com/antlr/antlr4/runtime/Go/antlr".NewBasicLexer
The function that you are trying to use does not exist.
In this case it is actually called NewBaseLexer as opposed to NewBasicLexer as you can see in the code

ReadFile returns nil when attempting to read file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am getting nil for sampleFlags in my debugger when attempting to read my file test.json. I am not sure if it is a path issue or where the reader pointer is. How FetchFlags is triggered is by a handler in server.go that ultimately calls FetchFlags.
flags.go
package flags
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type Flag struct {
Name string `json:"name"`
Category string `json:"category"`
Label string `json:"label"`
}
func FetchFlags() []Flag {
sampleFlags, _ := ioutil.ReadFile("test.json")
fmt.Printf("File contents: %s", sampleFlags)
var Flags []Flag
_ = json.Unmarshal(sampleFlags, &Flags)
return Flags
}
Structure:
/server
server.go
/package
/flags
flags.go
test.json
/pack_a
/pack_b
You are trying to open file relative to package path. This is a bad design approach. For example depending on compilation method Go may place binaries in $GOROOT/bin directory. And there will not be the test.json file.
Use absolute path for your file or use approaches from How can I open files relative to my GOPATH?.
The path should be relatve to your main.go(or equivalent) file and not your package.
(Absolute path should work as well but I am not 100% sure about that)
If you think about it then you will realize that package/flags is directly or indirectly imported into your main file.
Your code compilation/execution isn't jumping to package/flags, instead that code is imported to your main file.
I would recommend you to use abosulte path or path relative to your main file.

When naming interfaces, is it a good name if the ending is the same as the package name? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Which name is better, repository.UserRepository or repository.User?
I'm thinking about naming recently.
I have referenced several sources and they are talking.
Think about the context and name it.
Here is the link.
https://talks.golang.org/2014/names.slide#2
https://github.com/golang/go/wiki/CodeReviewComments
Additionally, Can you also recommend a project that can be referred to when creating an http server?
according to the golang official ducument
The importer of a package will use the name to refer to its contents, so exported names in the package can use that fact to avoid stutter. (Don't use the import . notation, which can simplify tests that must run outside the package they are testing, but should otherwise be avoided.) For instance, the buffered reader type in the bufio package is called Reader, not BufReader, because users see it as bufio.Reader, which is a clear, concise name. Moreover, because imported entities are always addressed with their package name, bufio.Reader does not conflict with io.Reader.
repository.User may be good then repository.UserRepository
the package in src/encoding/base64 is imported as "encoding/base64" not "encoding/base64Encoding"

Resources