YAML syntax-yaml turn Golang

Source: Internet
Author: User

Basic rules

YAML has the following basic rules:

1, Case sensitive

2. Use indentation to represent hierarchical relationships

3, prohibit the use of tab indentation, you can only use the space key

4. There is no limit to the indentation length, as long as the element alignment indicates that the elements belong to a hierarchy.

5. Use # to indicate comments

6. Strings can be marked without quotation marks

1, map, hash list

Use a colon (:) to denote a key-value pair, and all key-value pairs of the same indentation belong to a map, for example:

# Yaml says

Age:12

Name:huang

# The corresponding JSON representation

{' Age ': ' ' name ': ' Huang '}

You can also write a map on one line:

# Yaml says

{' Age ': 12,name:huang} # ' Age ' represents a string

# The corresponding JSON representation

{' Age ': ' ' name ': ' Huang '}

2, List, array

Use a hyphen (-) to indicate:

# Yaml says

-A

-B

-12

# corresponds to the JSON representation

[' A ', ' B ', 12]

It can also be written in one line:

# Yaml says

[A,b,c]

# corresponds to the JSON representation

[' A ', ' B ', ' C ']

3, scalar, pure quantity

The smallest unit of data can no longer be split.

Data structure nesting

The elements of map and list can be another map or list or a pure quantity. There are 4 common nesting of data

1. Map nested map

# Yaml says

Websites

YAML:yaml.org

ruby:ruby-lang.org

Python:python.org

Perl:use.perl.org

# corresponds to the JSON representation

{websites:

{YAML: ' yaml.org ',

Ruby: ' ruby-lang.org ',

Python: ' python.org ',

Perl: ' Use.perl.org '}}

2. Map nested list

# Yaml says

Languages

-Ruby

-Perl

-Python

-C

# corresponds to the JSON representation

{languages: [' Ruby ', ' Perl ', ' Python ', ' C ']}

3. List nested list

# Yaml says

-

-Ruby

-Perl

-Python

-

-C

-C + +

-Java

# corresponds to the JSON representation

[[' Ruby ', ' Perl ', ' Python '], [' C ', ' C + + ', ' Java ']

In addition, you can also indicate the structure as follows

# method 2

--Ruby

-Perl

-Python

--C

-C + +

-Java

# Method 3

-[Ruby,perl,python]

-[C,c++,java]

4. List nested map

# Yaml says

-

Id:1

Name:huang

-

Id:2

Name:liao

# corresponds to the JSON representation

[{id:1, Name: ' Huang '}, {id:2, Name: ' Liao '}]


Depth

#Block style represents the struct body

Block_style:

Clark:evans

Ingy:döt Net

Oren:ben-kiki

Such as:

Blockstyle struct{

Clark string Yaml: "Clark"

Ingy:string yaml: "ingy"

Oren:string yaml: "Oren"

} ' Yaml: 'block_style'

Yaml Turn Golang

Data, err: = Ioutil. ReadFile (./xxx.yaml)

If err! = Nil {

Entry. Witherror (ERR). PANICLN ("Failed to read file")

}

BS: = blockstyle{}

If err: = Yaml. Unmarshal (data, &bs); Err! = Nil {

Entry. Witherror (ERR). PANICLN ("Deserialization failed")

}

Related Article

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.