tags in Go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. ' Label ' is an optional parameter in the ' Break ' and ' continue ' statements, and is a mandatory argument in the ' goto ' statement. ' Label ' is only valid in the function that declares it. As long as the ' Label ' is declared in the function, it is valid throughout the scope of the function. "' Gofunc Main () {FMT. PRINTLN (1) goto ENDFMT.PRINTLN (2) end:fmt. PRINTLN (3)} "(Note!) We are the ' Label ' that is defined after the goto statement. shell>./bin/sandbox13 "The scope of the reference does not include the scope of the nested function: ' ' Gofunc () {fmt. PRINTLN ("Nested function") goto end} () End: "The above code will be compiled with the ' label End not defined ' error. In addition, it will be reported: ' label End defined and not used ', because we need to ensure that only the ' label ' needs to be declared. There is no concept of block scope in ' Label '. Therefore, you cannot declare a duplicate ' label ' in a nested block scope: ' ' Gogoto xx:{x:} ' compiles the above code to indicate that ' label ' already exists. The ' Label ' identifier and other identifiers have different namespaces, so there is no conflict with variable identifiers. The following code uses ' X ' as an identifier in both ' Label ' and variable declaration: ' Gox: = 1goto xx:fmt. PRINTLN (x) ' # # break statement ' break ' is generally used to jump out of ' for ' or ' switch ' statements. You can also use ' break ' in the ' SELECT ' statement in Go. For those with the C-class programming experience, it is certain that each ' case ' statement needs to end with ' break '. In most cases, however, the program does not want to execute into the next ' case ' statement. On the contrary, in Go, if the code that needs to execute the next clause can use the ' fallthrough ' statement: ' ' switch 1 {case 1:fmt. PRINTLN (1) Case 2:fmt. PRINTLN (2)}>./bin/sandbox1switch 1 {case 1:fmt. PRINTLN (1) fallthroUghcase 2:fmt. PRINTLN (2)}>./bin/sandbox12 "> Fallthrough can only be used as the last statement of a clause. The ' break ' statement cannot span a function boundary. "'" Gofunc f () {Break}func main () {for i: = 0; i <; i++ {f ()}} "" is reported as ' break was not in a loop ' error. In general, ' break ' can be used to jump out of ' for ', ' switch ' or ' SELECT ' statements. Use ' Label ' to get ' break ' to do more things. The ' Label ' of the ' break ' statement must correspond to ' for ', ' switch ' or ' SELECT ' statements around ' break '. So the following code cannot be compiled by: "' gofirstloop:for I: = 0; I < 10; i++ {}for I: = 0; I < 10; i++ {break Firstloop} ' it will be reported ' invalid break label Firstloop '. Because the ' Label ' is not on the loop that the break corresponds to. Break can jump out of nested loops: "' goouterloop:for I: = 0; I < 10; i++ {for J: = 0, J <, J + + (FMT). Printf ("I=%v, j=%v\n", I, J) break Outerloop} "" ">./bin/sandboxi=0, j=0" If ' break ' is used as follows, it is not just to exit the ' for ' Loop: ' Goswitchstatement:switch 1 {case 1:fmt. PRINTLN (1) for I: = 0; I < 10; i++ {break switchstatement}fmt. PRINTLN (2)}fmt. PRINTLN (3) ">/bin/sandbox13" "Break" can exit and switch to the specified ' Label ' anywhere in the ' for ', ' switch ' or ' SELECT ' statement. # # Continue statement He is similar to the ' break ' statement, but will go to the next iteration rather than exitRing (used only in loops): "' outerloop:for I: = 0; I < 3; i++ {for J: = 0; J < 3; J + + {FMT. Printf ("I=%v, j=%v\n", I, J) continue outerloop}}>./bin/sandboxi=0, J=0i=1, j=0i=2, j=0 ' # # goto statement ' goto ' statement allows the program to switch to a Continue execution where the label is labeled. "I: = 0start:fmt." PRINTLN (i) if I > 2 {goto End} else {i + = 1goto start}end: ' ' (Note: ' End ' can be null) differs from ' break ' and ' continue ' statements, ' goto ' statement must specify ' L Abel '. ' Goto ' can only move the program to a location in the same function. So there are two more rules to perform goto:-cannot skip variable declaration, if Goto skips, the compiler will report ' Goto done jumps over declaration of V at ... ' gogoto Donev: = 0done:fmt . Println (v) '-' goto ' cannot switch to another block of code, and if switched, the compiler will report ' Goto block jumps into the block starting at ... ': ' gogoto block{block:v: = 0fmt. Println (v)} "

via:https://medium.com/golangspec/labels-in-go-4ffd81932339

Author: Michałłowicki Translator: Saberuster proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

537 Reads

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.