Latest web development tutorials

Pergi struktur bahasa

Pergi berbagai bahasa dapat menyimpan jenis data yang sama, tetapi dalam struktur, kita dapat mendefinisikan item yang berbeda untuk jenis data yang berbeda.

Struktur terdiri dari serangkaian set data dari jenis yang sama atau dari berbagai jenis data merupakan.

Struktur merupakan rekor, seperti menyimpan catatan perpustakaan buku-buku, setiap buku memiliki sifat sebagai berikut:

  • Judul: Judul
  • Penulis: Penulis
  • Subjek: subjek
  • ID: Buku ID

Definisi struktur

Struktur didefinisikan dan membutuhkan penggunaan pernyataan tipe struct. struct akan menentukan tipe data baru, struktur memiliki satu atau lebih anggota. Pernyataan Jenis nama set struktur. Struktur Format adalah sebagai berikut:

type struct_variable_type struct {
   member definition;
   member definition;
   ...
   member definition;
}

Setelah Anda mendefinisikan jenis struktur, dapat digunakan untuk mendeklarasikan variabel, sintaks adalah sebagai berikut:

variable_name := structure_variable_type {value1, value2...valuen}

Untuk struktur

Jika Anda ingin mengakses anggota struktur, menggunakan dot operator, format (.) :. "Sebuah nama anggota struktur."

Ketik struktur variabel menggunakan definisi struct kata kunci, contoh adalah sebagai berikut:

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* 声明 Book1 为 Books 类型 */
   var Book2 Books        /* 声明 Book2 为 Books 类型 */

   /* book 1 描述 */
   Book1.title = "Go 语言"
   Book1.author = "www.w3big.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   Book2.subject = "Python 语言教程"
   Book2.book_id = 6495700

   /* 打印 Book1 信息 */
   fmt.Printf( "Book 1 title : %s\n", Book1.title)
   fmt.Printf( "Book 1 author : %s\n", Book1.author)
   fmt.Printf( "Book 1 subject : %s\n", Book1.subject)
   fmt.Printf( "Book 1 book_id : %d\n", Book1.book_id)

   /* 打印 Book2 信息 */
   fmt.Printf( "Book 2 title : %s\n", Book2.title)
   fmt.Printf( "Book 2 author : %s\n", Book2.author)
   fmt.Printf( "Book 2 subject : %s\n", Book2.subject)
   fmt.Printf( "Book 2 book_id : %d\n", Book2.book_id)
}

Contoh pelaksanaan hasil operasi di atas sebagai berikut:

Book 1 title : Go 语言
Book 1 author : www.w3big.com
Book 1 subject : Go 语言教程
Book 1 book_id : 6495407
Book 2 title : Python 教程
Book 2 author : www.w3big.com
Book 2 subject : Python 语言教程
Book 2 book_id : 6495700

Struktur sebagai parameter fungsi

Anda bisa seperti tipe data lain untuk jenis struktur dilewatkan ke fungsi sebagai parameter. Dan dengan cara contoh di atas untuk mengakses variabel struktur:

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* 声明 Book1 为 Books 类型 */
   var Book2 Books        /* 声明 Book2 为 Books 类型 */

   /* book 1 描述 */
   Book1.title = "Go 语言"
   Book1.author = "www.w3big.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   Book2.subject = "Python 语言教程"
   Book2.book_id = 6495700

   /* 打印 Book1 信息 */
   printBook(Book1)

   /* 打印 Book2 信息 */
   printBook(Book2)
}

func printBook( book Books ) {
   fmt.Printf( "Book title : %s\n", book.title);
   fmt.Printf( "Book author : %s\n", book.author);
   fmt.Printf( "Book subject : %s\n", book.subject);
   fmt.Printf( "Book book_id : %d\n", book.book_id);
}

Contoh pelaksanaan hasil operasi di atas sebagai berikut:

Book title : Go 语言
Book author : www.w3big.com
Book subject : Go 语言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.w3big.com
Book subject : Python 语言教程
Book book_id : 6495700

struktur pointer

Anda dapat menentukan pointer menunjuk ke struktur yang mirip dengan variabel pointer lain, format adalah sebagai berikut:

var struct_pointer *Books

Didefinisikan di atas dapat mengatasi memori pointer variabel struktur variabel. Lihat struktur alamat variabel, Anda dapat menempatkan ampersand di depan variabel struktur:

struct_pointer = &Book1;

Menggunakan pointer ke anggota struktur struktur, menggunakan operator "".:

struct_pointer.title;

Mari kita menggunakan struktur pointer menulis ulang contoh di atas, kode di bawah ini:

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* Declare Book1 of type Book */
   var Book2 Books        /* Declare Book2 of type Book */

   /* book 1 描述 */
   Book1.title = "Go 语言"
   Book1.author = "www.w3big.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   Book2.subject = "Python 语言教程"
   Book2.book_id = 6495700

   /* 打印 Book1 信息 */
   printBook(&Book1)

   /* 打印 Book2 信息 */
   printBook(&Book2)
}
func printBook( book *Books ) {
   fmt.Printf( "Book title : %s\n", book.title);
   fmt.Printf( "Book author : %s\n", book.author);
   fmt.Printf( "Book subject : %s\n", book.subject);
   fmt.Printf( "Book book_id : %d\n", book.book_id);
}

Contoh pelaksanaan hasil operasi di atas sebagai berikut:

Book title : Go 语言
Book author : www.w3big.com
Book subject : Go 语言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.w3big.com
Book subject : Python 语言教程
Book book_id : 6495700