網頁

2021/6/14

Golang struct 結構 簡單範例

Go語言的struct(結構)為包含多個屬性的資料型態,用來組織多個具有意義的資料。類似物件導向語言(e.g. Java, C++, C$)的類別(class)。


Struct型態使用struct關鍵字來定義,通常搭配type關鍵字使用。

例如下面定義一個Employee struct,裡面有三個欄位(field)分別為IDNameAge,然後在main()中以struct literal建立Employee並印出內容。

main.go

package main

import "fmt"

type Employee struct {
    ID int64
    Name string
    Age int
}

func main() {
    employee := Employee{1, "John", 33} // create by struct literal
    fmt.Println(employee) // {1 John 33}
}

Struct類似Java類別(Class)與實例(instance)的關係。下面是Java程式。

Demo.java

public class Demo {

    public static void main(String[] arges) {
        Employee employee = new Employee(1, "John", 33); // create instance of class Employee
        System.out.println(employee); // Employee{id=1, name='John', age=33}
    }

}

class Employee {
    private long id;
    private String name;
    private int age;

    public Employee(long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Employee{id=" + id + ", name='" + name + "', age=" + age +"}";
    }
}

或是類似JavaScript的Object。下面是JavaScript程式。

demo.js

// define Employee object type by constructor function
function Employee(id, name, age) {
  this.id = id;
  this.name = name;
  this.age = age;
}

let employee = new Employee(1, 'John', 33); // create Employee object
console.log(employee); // Employee {id: 1, name: "John", age: 33}


Struct的屬性使用點號.存取。

main.go

package main

import "fmt"

type Employee struct {
    ID int64
    Name string
    Age int
}

func main() {
    employee := Employee{1, "John", 33}
    fmt.Println(employee) // {1 John 33}
    
    employee.Age = 26
    fmt.Println(employee.Age) // 26
}

也可以先建立一個空的struct然後才設定屬性值。

main.go

package main

import "fmt"

type Employee struct {
    ID int64
    Name string
    Age int
}

func main() {
    var employee Employee // create an empty struct Employee
    fmt.Println(employee) // {0 0}

    employee.Id = 1
    employee.Name = "John"
    employee.Age = 33
    fmt.Println(employee) // {1 John 33}
}

在建立struct時可以指定屬性名稱(不用依照順序)並給於值(稱為struct literals),每個屬性後面要加上逗號,

main.go

package main

import "fmt"

type Employee struct {
    ID int64
    Name string
    Age int
}

func main() {
    employee := Employee{
        ID: 1,        // don't forget add comma ',' after each value assinment of the field
        Name: "John",
        Age: 33,      // even the last field assinment have to add comma at the end 
    }
    fmt.Println(employee) // {1 John 33}
}

Struct也可以匿名的方式定義及使用,例如下面宣告並建立一個匿名struct。

main.go

package main

import "fmt"

func main() {
    employee := struct { // anonymous struct
        ID int64
        Name string
        Age int
    } {
        ID: 1,
        Name: "John",
        Age: 33,
    }

    fmt.Println(employee) // {1 John 33}
}

類似JavaScript以Object literal建立物件。下面是JavaScript程式碼。

demo.js

let employee = {
  id: 1,
  name: 'John',
  age: 33 
}

console.log(employee); // {id: 1, name: "John", age: 33}

Struct裡面的屬性也可以是struct。例如下面Employee的屬性的Dept屬性為Department struct型態。

main.go

package main

import "fmt"

type Employee struct {
    ID int64
    Name string
    Age int
    Dept Department // nested struct
}

type Department struct {
    ID int64
    Name string
}

func main() {
    employee := Employee{1, "John", 33, Department{2, "HR"}} // create by struct literal
    fmt.Println(employee) // {1 John 33 {2 HR}}
}


沒有留言:

張貼留言