Home  >  Article  >  Backend Development  >  Golang Struct Field Scopes

Golang Struct Field Scopes

王林
王林Original
2024-08-31 20:30:411172browse

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

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
}

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)
}

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 = authoer
  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

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

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
}

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)
}

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

  • title: "Book Title

Golang Struct Field Scopes

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

The above is the detailed content of Golang Struct Field Scopes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn