Latest web development tutorials

Ir estructura del lenguaje

Ir variedad de idiomas puede almacenar el mismo tipo de datos, sino en la estructura, podemos definir diferentes artículos para diferentes tipos de datos.

La estructura se compone de una serie de conjuntos de datos del mismo tipo o de diferentes tipos de datos constituidos.

Estructura representa un registro, como guardar una biblioteca de registro de libros, cada libro tiene las siguientes propiedades:

  • Título: Título
  • Autor: Autor
  • Asunto: Asunto
  • ID: Libros ID

Definición de la estructura

Estructura se define y se requiere el uso de la declaración de tipo de estructura. struct definirá un nuevo tipo de datos, la estructura tiene uno o más miembros. declaración de nombre de tipo establece la estructura. estructura de formato es el siguiente:

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

Una vez definido el tipo de estructura, que puede utilizarse para declarar las variables, la sintaxis es la siguiente:

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

Para una estructura de

Si desea acceder a un miembro de la estructura, utilice el operador punto, el formato (.) :. "Un nombre de miembro de estructura."

Tipo de estructura variable usando las definiciones de la palabra clave struct, los ejemplos son los siguientes:

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)
}

Los ejemplos de la aplicación de los anteriores resultados operativos de la siguiente forma:

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

La estructura como un parámetro de función

Puede igual que otros tipos de datos en el tipo de estructura pasa a la función como un parámetro. Y a modo de ejemplo para acceder a la variable de estructura:

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);
}

Los ejemplos de la aplicación de los anteriores resultados operativos de la siguiente forma:

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

estructura del puntero

Se puede definir un puntero que apunta a una estructura similar a otra variable puntero, el formato es el siguiente:

var struct_pointer *Books

Se ha definido anteriormente puede abordar variables de estructura variable de puntero de memoria. Ver estructura de dirección variable, se puede colocar un símbolo de unión frente a las variables de estructura:

struct_pointer = &Book1;

El uso de un puntero a un miembro de estructura de una estructura, usar el operador ".":

struct_pointer.title;

Vamos a usar el puntero estructura de reescribir el ejemplo anterior, el código de abajo:

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);
}

Los ejemplos de la aplicación de los anteriores resultados operativos de la siguiente forma:

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