Go語言程式建立簡單類?


在本教程中,我們將討論在 Go 程式設計中建立簡單類的方法。

但是,在編寫程式碼之前,讓我們簡要討論一下類的概念。

什麼是類?

類是建立物件的模板或藍圖。類將程式的所有元素繫結在一起,它是面向物件程式設計的重要組成部分。

然而,Go 語言**不支援“class”關鍵字**,這與其他面嚮物件語言(如 Java、C++ 等)不同。

但是,這並不限制 Go 使用類的功能。Go 在不使用“class”關鍵字的情況下保持了類的概念。

Go 語言中的 Struct 關鍵字

  • Struct 關鍵字是 Go 中一個強大的工具,類似於類。這意味著我們可以使用 struct 在 Go 中建立類,而無需實際使用“class”關鍵字。

  • 透過定義 struct,我們可以定義一個類,並且透過使用 struct 型別,我們還可以實現其方法。Struct 關鍵字的行為與類非常相似。

  • Struct,也稱為結構體,是將不同的欄位組合成一個欄位的集合。它們用於將資料組合為一個。

  • Struct 是使用者定義的且可變的。

示例

假設您想儲存組織中員工的詳細資訊。這些詳細資訊將包括員工姓名、員工 ID、地址、性別、年齡等不同資料型別的欄位。

為了儲存不同資料型別的多個值,可以使用 struct,其中 Employee 可以是 struct 名稱,所有員工的詳細資訊可以是其欄位。

type Employee struct {
   employeeName   string
   employeeID     int
   employeeEmail  string
   employeeSalary float
}

簡單類的演示

建立 Employee Struct 來模擬類

演算法

  • **步驟 1** - 建立一個名為“Employee”的 struct,並宣告其欄位(如員工姓名、員工 ID 等)及其資料型別。

  • **步驟 2** - 建立一個函式 PrintDetailsOfEmployee(),它將訪問 Employee struct 的值並列印員工的詳細資訊。

  • **步驟 3** - 在主方法中,將值初始化為員工。這將以鍵值對的形式完成。

  • **步驟 4** - 透過呼叫 PrintDetailsOfEmployee() 函式列印員工的詳細資訊。

示例

package main

// fmt package allows us to print formatted strings
import "fmt"

// defining struct
type Employee struct {
	employeeName   string
	employeeID     int
	employeeGender string
	employeeEmail  string
	employeeSalary float32
}

// this method will access the values of struct Employee
func (emp Employee) PrintDetailsOfEmployee() {
	fmt.Println("Name : ", emp.employeeName)
	fmt.Println("ID: ", emp.employeeID)
	fmt.Println("Gender: ", emp.employeeGender)
	fmt.Println("Email Address : ", emp.employeeEmail)
	fmt.Println("Salary : ", emp.employeeSalary)
}

func main() {
     fmt.Println("Employee Details \n")
     // storing employee details
	var emp1 = Employee{employeeName: "Rahul Singh", employeeID: 50062, employeeGender: "Male", employeeEmail: "rahul@hello.com", employeeSalary: 65000}
	var emp2 = Employee{employeeName: "Shreya Goel", employeeID: 50132, employeeGender: "Female", employeeEmail: "shreya@hello.com", employeeSalary: 57000}

     // printing employee details
	emp1.PrintDetailsOfEmployee()
	emp2.PrintDetailsOfEmployee()
}

輸出

Employee Details 

Name :  Rahul Singh
ID:  50062
Gender:  Male
Email Address :  rahul@hello.com
Salary :  65000

Name :  Shreya Goel
ID:  50132
Gender:  Female
Email Address :  shreya@hello.com
Salary :  57000

程式碼描述

  • var emp1 = Employee{employeeName: "Rahul Singh", employeeID: 50062, employeeGender: "Male", employeeEmail: "rahul@hello.com", employeeSalary: 65000} − 這裡,我們以鍵值對的形式宣告 Employee 的值,其中鍵是員工欄位,值是員工的資訊。

建立 Rectangle Struct 來模擬類

  • **步驟 1** - 建立一個名為“Rectangle”的 struct,並宣告其欄位(如長度和寬度)及其資料型別。

  • **步驟 2** - 建立一個函式 CalculateArea() 來列印矩形的詳細資訊,並使用公式長度 * 寬度計算其面積並列印。

  • **步驟 3** - 在主方法中,將值初始化為矩形 struct,並以鍵值對的形式儲存其值。

  • **步驟 4** - 透過呼叫 CalculateArea() 函式列印矩形的長度、寬度和麵積。

示例

package main

// fmt package allows us to print formatted strings
import "fmt"

// defining struct
type Rectangle struct {
	length  int
	breadth int
}

// this method will access the values of struct Rectangle
func (rect Rectangle) CalculateArea() {
	fmt.Println("Length : ", rect.length)
	fmt.Println("Breadth : ", rect.breadth)
	fmt.Println("Therefore, Area : ", rect.length*rect.breadth)
}

func main() {
	fmt.Println("Rectangle Details \n")
	var rect1 = Rectangle{length: 5, breadth: 2}
	var rect2 = Rectangle{length: 25, breadth: 10}

	rect1.CalculateArea()
	rect2.CalculateArea()
}

輸出

Rectangle Details 

Length :  5
Breadth :  2
Therefore, Area :  10

Length :  25
Breadth :  10
Therefore, Area :  250

程式碼描述

  • var rect1 = Rectangle{length: 5, breadth: 2} − 這裡,我們以鍵值對的形式宣告 Rectangle 的值,其中鍵是矩形欄位,值是矩形的資訊。

結論

這就是在 Go 程式設計中建立類的方法,我們透過兩個示例詳細說明了 Go 中沒有“class”關鍵字如何不限制我們使用類的功能。您可以使用這些教程探索更多關於 Go 程式設計的資訊。

更新於: 2022-12-28

693 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.