Published
- 4 min read
Go Facts 1
package main
- The
package main
is a special package in Go that tells the Go compiler that this is the main package for your program. - The
package main
is required for any Go program that you want to run as an executable. - The
package main
must contain afunc main()
function, which is the entry point for your Go program. - The
package main
is where you put the code you want to run when your program starts. - The
package main
is the only package that can contain thefunc main()
function.
// example of package main
package main
// main function
func main() {
fmt.Println("Hello, World!")
}
Blank Identifier
- The blank identifier is a special identifier in Go that can be used to store values that you don’t need.
- It’s a way to tell Go that you don’t care about the value returned by a function or assigned to a variable.
- The blank identifier is represented by an underscore
_
. - It’s often used when you need to call a function that returns a value, but you don’t need that value.
- The blank identifier can also be used to import a package solely for its side effects.
- This is done by importing the package with the
_
prefix. - This tells Go that you don’t need to use any of the package’s functions or variables directly, but you still want to import it for its side effects.
- For example, if you want to import a package that registers itself with the
database/sql
package, you can use the blank identifier to import it without using any of its functions or variables. - This is a common pattern in Go for packages that need to register themselves with the standard library or other packages.
// blank identifier with a function call example
_, err := fmt.Println("Hello, World!")
if err != nil {
fmt.Println("Error:", err)
}
// blank identifier with a variable assignment example
_ = "Hello, World!"
// blank identifier with a package import example
import _ "github.com/go-sql-driver/mysql"
func main()
- The main function is the entry point for your Go program.
- When you run your Go code, the main function is where execution begins.
- The main function is required in the
main
package. - The main function takes no arguments and returns nothing.
- The main function is where you put the code you want to run when your program starts.
func main() {
fmt.Println("Hello, World!")
}
init function
- init() function is a special function in Go that is called when a package is imported.
- You can use the init() function to perform any initialization that needs to be done when a package is imported.
- The init() function is called automatically by Go when a package is imported, so you don’t need to call it yourself.
- You can have multiple init() functions in a package, and they will be called in the order they are declared.
- The init() function is often used to set up global variables or perform other initialization tasks that need to be done when a package is imported.
- The init() function is called before the main function, so any initialization that needs to be done before the main function is called can be done in the init() function.
- The init() function is a good place to set up database connections, read configuration files, or perform other initialization tasks that need to be done before the main function is called.
- The init() function is a great way to keep your code organized and ensure that initialization tasks are done when they need to be done.
// init function example
func init() {
fmt.Println("Initializing...")
}
// init function with multiple init functions example
func init() {
fmt.Println("Initializing 1...")
}
func init() {
fmt.Println("Initializing 2...")
}
// init function with database connection example
func init() {
var err error
db, err = sql.Open("mysql", "user:password@/dbname")
if err != nil {
log.Fatal(err)
}
}
// init function with configuration file example
func init() {
config, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(config, &cfg)
if err != nil {
log.Fatal(err)
}
}