Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

How to access the struct from another package in Go (Golang)

Posted on April 4, 2023April 4, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

The struct name in another package must start with a capital letter so that it is public outside its package. If the struct name starts with lowercase then it will not be visible outside its package.

To access a struct outside its package we need to import the package first which contains that struct.

Program

Here is the code for the same

go.mod

module sample.com/learn
go 1.16

model/person.go

package model
type Person struct {
Name string
}

main.go

package main
import (
"fmt"
"sample.com/learn/person"
)
func main() {
p := &model.Person{
Name: "John",
}
fmt.Println(p.Name)
}

In this program, we first import the model package from the main package as below.

"sample.com/learn/model"

Then we access the Person struct as below from the main package

p := &model.Person{
Name: "John",
}

This works because Person struct name is in uppercase.

Let’s change the struct name to lower case and run this program. It gives the below compilation error

cannot refer to unexported name model.person

Also, check out our Golang comprehensive tutorial Series – Golang Comprehensive Tutorial

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP