Updated Object methods explanation. (#18933)

Fixed Object methods explanation and code samples to be more precise.
This commit is contained in:
Harsha Gudladona
2018-10-15 17:33:29 -04:00
committed by Quincy Larson
parent 086758250b
commit b9520deece

View File

@ -22,6 +22,8 @@ To initialise a new object, we can use the Go shorthand syntax for creating and
```go ```go
func main() { func main() {
type MyInt int64
// Create a user and set both the first and last name properties // Create a user and set both the first and last name properties
user1 := User{ user1 := User{
FirstName: "John", FirstName: "John",
@ -36,9 +38,13 @@ func main() {
### Object methods ### Object methods
Go enables assigning methods to structs. This enables grouping of relevant operations to the data it affects. In this example we will write a method on the `User` struct to generate the full name of the user: Go enables declaring methods to struct types and non struct types. This enables grouping of relevant operations to the data it affects. In this example we will write a method on the `User` struct to generate the full name of the user and String method on `MyInt` type to return a String:
```go ```go
func (myint MyInt) String() string {
return fmt.Sprintf("%d", myint)
}
func (u User) FullName() string { func (u User) FullName() string {
return strings.Join([]string{u.FirstName, u.LastName}, " ") return strings.Join([]string{u.FirstName, u.LastName}, " ")
} }
@ -47,7 +53,7 @@ func (u User) FullName() string {
This method will join the first and last name of the user with a space in between. Calling the method might look like this: This method will join the first and last name of the user with a space in between. Calling the method might look like this:
```go ```go
println(user1.FullName()) fmt.println(user1.FullName())
``` ```
### Struct Tags ### Struct Tags
@ -79,12 +85,14 @@ type User struct {
} }
``` ```
Doing this will make the following code throw an error at build time as it is trying to interact with an unexported property: Doing this will make the following code throw a compilation error as it is trying to assign value to an unexported property:
```go ```go
user1.email = "john@wick.com" user1.email = "john@wick.com"
``` ```
Same principle applies when attempting to read data from an unexported property.
This also applies to methods: This also applies to methods:
```go ```go
@ -125,5 +133,5 @@ func main() {
user1.SetEmail("john@wick.com") user1.SetEmail("john@wick.com")
// Access and print the user's email address // Access and print the user's email address
println(user1.Email()) fmt.println(user1.Email())
} }