Golang Struct Field Scopes

王林
Release: 2024-08-31 20:30:41
Original
937 people have browsed it

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
Copy after login

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
}
Copy after login

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)
}
Copy after login

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
Copy after login

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
Copy after login

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
}
Copy after login

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)
}
Copy after login

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
Copy after login

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

source:dev.to
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!