A very basic example showcasing the use of go module and struct for OOP in Golang project
-
Why the import module is "GoOOP/demo/printer" instead of just "printer" or "Golang-OOP-Example/printer"?
It is because this project is initialized with
go mod init GoOOP/demo
-
Why the compiled binary is named "demo.exe" instead of the folder name "Golang-OOP-Example.exe"?
It is because this project is initialized with
go mod init GoOOP/demo
and the go build command will use the last section of the module name as the binary executable name.
-
If I am starting a new project, how can I name my module?
Use the go mod command as follows.
go mod init {your module name} /* Example of module names: github.com/tobychui/mymodule mydomain.com/mymodule anything/you/want */ go mod tidy
-
Why the module has to be initialize with
p := printer.NewPrinter("Tim")
instead of other module name?You can choose to import "GoOOP/demo/printer" with other names like
import ( magic "GoOOP/demo/printer" )
and call to the imported module using
p := magic.NewPrinter("Tim")
Feel free to open a new issue and I will add your Q&A into the README.md file.