<\/span><\/h1>\n\n\n\nLet’s see a program for the same.<\/p>\n\n\n\n
go.mod<\/strong><\/p>\n\n\n\nmodule sample\n\ngo 1.14<\/code><\/pre>\n\n\n\nsample\/b\/b1.go<\/strong><\/p>\n\n\n\npackage b\n\nimport (\n\t\"fmt\"\n)\n\nfunc init() {\n\tfmt.Println(\"Init: b1\")\n}\n\nfunc TestB() error {\n\treturn nil\n}<\/code><\/pre>\n\n\n\nsample\/b\/b2.go<\/strong><\/p>\n\n\n\npackage b\n\nimport (\n\t\"fmt\"\n)\n\nfunc init() {\n\tfmt.Println(\"Init: b2\")\n}<\/code><\/pre>\n\n\n\nsample\/a\/a1.go<\/strong><\/p>\n\n\n\npackage a\n\nimport (\n\t\"fmt\"\n\t\"sample\/b\"\n)\n\nfunc init() {\n\tfmt.Println(\"Init: a1\")\n}\n\nfunc TestA() error {\n\treturn b.TestB()\n}<\/code><\/pre>\n\n\n\nsample\/a\/a2.go<\/strong><\/p>\n\n\n\npackage a\n\nimport (\n\t\"fmt\"\n)\n\nfunc init() {\n\tfmt.Println(\"Init: a2\")\n}<\/code><\/pre>\n\n\n\nsample\/main.go<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sample\/a\"\n)\n\nfunc init() {\n\tfmt.Println(\"Init: main\")\n}\nfunc main() {\n\tfmt.Println(\"Main Function Executing\")\n\ta.TestA()\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nInit: b1\nInit: b2\nInit: a1\nInit: a1\nInit: main\nMain Function Executing<\/code><\/pre>\n\n\n\nNotice in above example that init function in source files of package b<\/strong> are run first. Then init function in source files of package a<\/strong> is run and then init function in source file of main package is run. After that main function is run<\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewExample Overview Below the order of execution of a go program. The program starts with the main package. All imported packages in the source files of the main package…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-4340","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Order of execution of a Go program - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n