Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
YouTube Security VideosNutanix advances legacy and AI app management with AMD(01.10.2026 um 16:00 Uhr)
•
YouTube Security VideosPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••••
Videos & KonferenzenPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••
Sicherheitslücken (CVE)USN-8857-1: KCoreAddons vulnerability(01.10.2026 um 12:48 Uhr)
•••
YouTube Security VideosNutanix advances legacy and AI app management with AMD(01.10.2026 um 16:00 Uhr)
•
YouTube Security VideosPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••••
Videos & KonferenzenPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••
Sicherheitslücken (CVE)USN-8857-1: KCoreAddons vulnerability(01.10.2026 um 12:48 Uhr)
•••
Intelligence View
⚡ tsecurity.de Intelligence

How to Import Packages in Go

When a Go project grows larger, it becomes necessary to access functions and variables from different packages. In Go, we can do this using the import…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

When a Go project grows larger, it becomes necessary to access functions and variables from different packages. In Go, we can do this using the import statement.



Project Structure Example



Consider we have a project structure as below:



MainFolder/

│── FolderA/

│── FolderB/

│── FolderC/

│── main.go

│── go.mod

│── go.sum





FolderA has a function → FunctionA

FolderB has a function → FunctionB

FolderC has a function → FunctionC







Importing a Package in main.go



If we need to use FunctionA inside main.go, we import the package as follows:




package main

import (
"MainFolder/FolderA" // Importing FolderA package
)

func main() {
// Now we can access functions from FolderA
FolderA.SampleFunction2() // ✅ This works because it starts with an uppercase letter
FolderA.sampleFunction1() // ❌ Error: Unexported function (starts with lowercase)
}






This rule also applies to struct fields:




type User struct {
Name string // Exported
age int // Unexported
}







*Exported vs Unexported Functions

*


Functions that start with an uppercase letter (SampleFunction2()) are exported and can be accessed from other packages.



Functions that start with a lowercase letter (sampleFunction1()) are unexported and can only be accessed within the same package.



*Example: Correct Function Declarations in FolderA/a.go

*





package FolderA // Package name should match the folder name

// Exported function (accessible from other packages)
func SampleFunction2() {
// Function logic
}

// Unexported function (only accessible within the same package)
func sampleFunction1() {
// Function logic
}






Accessing Another Package from FolderA (Using FolderB's Function in FolderA)



Now, let's say we need to call FunctionB from FolderB inside FolderA/a.go:



Correct way to import and use FunctionB in FolderA/a.go



package FolderA




import "MainFolder/FolderB"

func sampleFunction1() {
FolderB.FunctionB() // ✅ Correct usage
}







*Accessing Global Variables from Another Package

*


If FolderC contains a global variable like DbPass, we can access it from FolderA as follows:



Example: Using Global Variables from FolderC in FolderA/a.go




package FolderA

import (
"MainFolder/FolderB"
"MainFolder/FolderC"
)

func sampleFunction1() {
FolderB.FunctionB() // ✅ Function call

// Accessing a global variable from FolderC
if FolderC.DbPass == "some value" { // ✅ Variable must start with an uppercase letter
// Do something
}
}






Key Takeaways




  • Package Naming Conventions: The package name should match the folder name (e.g., package FolderA inside FolderA/).

  • Exported Functions and Variables: Functions and variables that need to be accessed from other packages must start with an uppercase letter.

  • Lowercase Functions and Variables: Functions and variables that start with lowercase letters are private to the package.

  • Importing a Package: Use import "MainFolder/FolderX" to access other packages in the project.

  • Calling a Function from Another Package: Use PackageName.FunctionName() syntax.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to Import Packages in Go

Thematisch verwandte Begriffe: Import, Packages · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag