Go語言建立類和物件
本文我們將學習如何建立類和物件。
結構體(Structs) − Go語言沒有類。要在Go語言中建立物件,我們可以指定結構體並在其中儲存鍵值對。結構體是一種使用者自定義的資料型別,用於將資料組合在一起。儲存的值可以具有相同或不同的資料型別。
語法
定義結構體的語法如下:
type name_of_struct struct {
name_1 type_1
name_2 type_2
name_3 type_3
name_4 type_4 …
}
一個新的結構體以`type`關鍵字開頭,用於指定我們正在定義一個新型別,然後是結構體的名稱和`struct`關鍵字,用於指定我們正在定義一個新結構體。結構體可以將相同或不同資料型別的不同值儲存在一起。
物件 − 物件用於以鍵值對的形式將資料封裝在一起。建立物件的方法有很多種。我們將在本文中討論其中一些方法。
示例1
Go語言建立類程式碼:
package main
// fmt package allows us to print anything on the screen
import "fmt"
// defining a structure with name myStrunct
type Student struct {
// defining values of struct
name string
rollNo int
admissionNo int
class int
}
// function to student data type
func (s Student) printDetails() {
fmt.Println("Student Details...\n")
// printing the structure details
fmt.Println("Student name is:", s.name)
fmt.Println("Student Admission number is:", s.admissionNo)
fmt.Println("Student Class is:", s.class)
fmt.Println("Student Roll number is:", s.rollNo)
}
func main() {
// defining a variable name stud_1 of type Student
var stud_1 = Student{name: "Nitin Sharma", rollNo: 21, class: 7, admissionNo: 2485}
// calling the printDetails() function to print the structure details on the screen.
stud_1.printDetails()
}
輸出
Student Details... Student name is: Nitin Sharma Student Admission number is: 2485 Student Class is: 7 Student Roll number is: 21
以上程式碼描述
首先,我們需要匯入`fmt`包。此包允許我們在螢幕上列印任何內容。
然後我們定義了一個名為`Student`的結構體,它以字串和整數的形式儲存學生的憑據,例如姓名、學號、班級和入學號。
然後我們定義結構體應具有的屬性以及它應採用的資料型別。
接下來,我們定義了一個函式,該函式以`student`型別的變數作為引數,並使用`fmt.Println()`函式在螢幕上列印學生的詳細資訊。
呼叫`main()`函式。
建立一個`stud_1`變數,並在其中儲存`Student`型別的鍵值對。
呼叫`printDetails()`函式。
使用`fmt.Println()`函式在螢幕上列印詳細資訊。
示例2:Go語言建立物件
獲得結構體後,下一步是從結構體中建立物件。有多種方法可以從結構體建立物件。
方法1:向結構體傳遞逗號分隔的值
獲得結構體後,下一步是從結構體中建立物件。有多種方法可以從結構體建立物件。
示例
建立物件最簡單的方法是按照在結構體中定義的順序直接傳遞值。
package main
import "fmt"
// fmt package allows us to print anything on the screen
// defining a structure named Employee and adding some data to it
type Employee struct {
Name string
Age int
Designation string
Salary int
}
// calling the main() function
func main() {
// creating an object named newEmp by passing various comma
//separated values to it.
var newEmp = Employee{"Nitin", 27, "Developer", 40}
// printing the age of the employee from the newEmp object
fmt.Println("The age of the employee is:", newEmp.Age)
// printing the name of the employee from the newEmp object
fmt.Println("The name of the employee is:", newEmp.Name)
}
輸出
The age the of employee is: 27 The name the of employee is: Nitin
描述
首先,我們匯入`fmt`包,它允許我們在螢幕上列印任何內容。
接下來,我們建立一個名為`Employee`的結構體,並在其中定義鍵,例如姓名、年齡、薪水等。
然後我們呼叫`main()`函式。
從`Employee`類建立一個名為`newEmp`的物件,並將值作為逗號分隔的值傳遞給鍵。
現在,我們的`newEmp`物件包含我們可以使用`fmt.Println()`函式在螢幕上列印的所有必要資料。
要訪問物件的任何屬性,我們需要在指定物件名稱後使用“.”表示法,後跟我們要訪問的屬性。
注意 − 如果我們在建立物件時未指定結構體中定義的任何屬性的值,則會為這些屬性選擇一些預設值。以下是一些預設值。
字串值預設為空字串“”。
整數值預設為0。
布林值預設為false。
方法2:使用`new`關鍵字建立物件
示例
建立物件的另一種常用方法是使用`new`關鍵字。讓我們看一個例子以瞭解更多資訊。
package main
import "fmt"
// fmt package allows us to print anything on the screen
// defining a structure named Employee and adding some data to it
type Employee struct {
Name string
Age int
Designation string
Salary int
}
// calling the main() function
func main() {
// creating an empty object named newEmp.
var newEmp = new(Employee)
// assigning values to newEmp object
newEmp.Name = "Ram"
newEmp.Age = 30
// printing the age of the employee from the newEmp object
fmt.Println("The age of employee is:", newEmp.Age)
// printing the name of the employee from the newEmp object
fmt.Println("The name of employee is:", newEmp.Name)
// printing the default values
fmt.Println("The default values for salary is:", newEmp.Designation, newEmp.Salary)
}
輸出
The age of employee is: 30 The name of employee is: Ram The default values for salary is: 0
結論
我們已經成功編譯並執行了Go語言程式碼來建立類和物件,並附帶示例。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP