From b9520deece2cfebacc97089b045a24d8e6f43d22 Mon Sep 17 00:00:00 2001 From: Harsha Gudladona <7864088+gudladona87@users.noreply.github.com> Date: Mon, 15 Oct 2018 17:33:29 -0400 Subject: [PATCH] Updated Object methods explanation. (#18933) Fixed Object methods explanation and code samples to be more precise. --- .../pages/guide/english/go/go-structs/index.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/client/src/pages/guide/english/go/go-structs/index.md b/client/src/pages/guide/english/go/go-structs/index.md index 6c0fcf1dd1..a962793951 100644 --- a/client/src/pages/guide/english/go/go-structs/index.md +++ b/client/src/pages/guide/english/go/go-structs/index.md @@ -22,6 +22,8 @@ To initialise a new object, we can use the Go shorthand syntax for creating and ```go func main() { + type MyInt int64 + // Create a user and set both the first and last name properties user1 := User{ FirstName: "John", @@ -36,9 +38,13 @@ func main() { ### 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 +func (myint MyInt) String() string { + return fmt.Sprintf("%d", myint) +} + func (u User) FullName() string { 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: ```go - println(user1.FullName()) + fmt.println(user1.FullName()) ``` ### 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 user1.email = "john@wick.com" ``` +Same principle applies when attempting to read data from an unexported property. + This also applies to methods: ```go @@ -125,5 +133,5 @@ func main() { user1.SetEmail("john@wick.com") // Access and print the user's email address - println(user1.Email()) + fmt.println(user1.Email()) }