DEV Community

Nerdherd
Nerdherd

Posted on

Golang Struct Field Scopes

Struct Field Scopes

Exported Fields

In other languages, this would be similar to the public access qualifier.

  • If you come from Ruby like me, this would be defining attributes with attr_accessor

If the field (i.e. attribute) of the struct starts with an upper case, it would mean that that field is exported, thus accessible outside of the package.

Assume we have the following files in Go project:



main.go
/library
  /book.go


Enter fullscreen mode Exit fullscreen mode

We would define book.go in it's own package.



// library/book.go

// Assume we have a package called "library" which contains a book.
package library

// Struct that represents a physical book in a library with exported fields
type Book struct {
  Title string
  Author string
}


Enter fullscreen mode Exit fullscreen mode

When using it in main.go:



package main

import (
  "fmt"
  "library" // importing the package that the struct Book is in
)

func main() {
  book := library.Book{
    Title: "Book Title",
    Author: "John Snow"
  }
  // Print the title and author to show that the struct Book fields are accessible outisde it's package "library"
  fmt.Println("Title:", book.Title)
  fmt.Println("Author:", book.Author)
}


Enter fullscreen mode Exit fullscreen mode

In Ruby, this would be synonymous with using attr_accessor since we can:

  • read and write the attribute values outside of the class


class Book
  # allow read and write on the attributes from outside the class
  attr_accessor(:title, :author)

  def initalize(title = nil, author = nil)
    @title  = title
    @author = author
  end
end

# usage outside of the class
book = Book.new()

# assinging attributes outside of the class
book.title = "Book Title"
book.title = "Jon Snow"

# accessing attributes outside of the class
puts book.title, book.author


Enter fullscreen mode Exit fullscreen mode

Private Fields

This is similar to private access qualifiers in other languages

If it starts with a lower case, the fields will not be accessible.

Try it for yourself!

Assuming your module name is myapp in go.mod



// go.mod
module myapp

go 1.22.5


Enter fullscreen mode Exit fullscreen mode

We create a new file in library/book.go under the package library



// library/book.go

// Assume we have a package called "library" which contains a book.
package library

// Fields start with lowercase, fields are not exported
type Book struct {
  title string
  author string
}


Enter fullscreen mode Exit fullscreen mode

Import the package into main.go



// main.go
package main

import (
  "fmt"
  // import the library package
  "myapp/library"
)

func main() {
  book := library.Book{
    title: "Book Title",
    author: "John Snow"
  }
  // Print the title and author to show that the struct Book fields are accessible outisde it's package "library"
  fmt.Println("title:", book.title)
  fmt.Println("author:", book.author)
}


Enter fullscreen mode Exit fullscreen mode

If you have Go setup in VSCode, you would get the following lint error:

unknown field author in struct literal of type library.Bookcompiler[MissingLitField](https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#MissingLitField

Golang lint error when struct fields is not exported

Top comments (2)

Collapse
 
erikkalkoken profile image
Erik Kalkoken

Thank you for providing this guide.

Just a small typo I noticed: In the first type definition of Book there is a comma after the Title field, which is not valid syntax in Go.

Collapse
 
nerdherd profile image
Nerdherd

Thanks for point that out @erikkalkoken ! fixed the typo.