Golang syntax exploration-declaring struct body

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1 Anonymous declaration.

Tests: = []struct {
Key, Val string
OK bool
}{
{"Имя", "V", false},//key must be ASCII
{"Name", "Валю", true},//value May NON-ASCII
{"", "V", false},//key must be non-empty
{"K", "", true},//value may empty
}

This pattern is commonly used. Define a constant table, the format of the table can be used in this place.

2 Collective statement:

Type (
Echo is the top-level framework instance.
Echo struct {
Server engine. Server
Premiddleware []middlewarefunc
Middleware []middlewarefunc
Maxparam *int
Notfoundhandler Handlerfunc
Httperrorhandler Httperrorhandler
Binder Binder
Renderer renderer
Pool sync. Pool
debug bool
Router *router
Logger log. Logger
}

Route contains a handler and information for matching against requests.
Route struct {
Method string
Path string
Handler string
}

Httperror represents an error, occurred while handling a request.
Httperror struct {
Code int
Message string
}

**************

than a single type XXX struct province things up. The Golang is not verbose.

The similar things are:

Import, Var, and const statements. This reduces the noise signal in the middle of the program.

For example, this is the case with Python, Java. Write an import every time.
Import Email.parser
Import Email.message
Import http
Import IO
Import OS
Import re
Import socket
Import Collections
From Urllib.parse import Urlsplit

This seemingly small thing is actually very useful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.