YouTip LogoYouTip

Go Basic Syntax

# Go Basic Syntax In the previous chapter, we learned about the basic components of a Go program. In this chapter, we will learn the basic syntax of Go. * * * ## Go Tokens A Go program can consist of multiple tokens. A token can be a keyword, an identifier, a constant, a string, or a symbol. For example, the following Go statement consists of 6 tokens: fmt.Println("Hello, World!") The 6 tokens are (one per line): 1. fmt 2. .3. Println4. (5. "Hello, World!"6. ) * * * ## Line Separators In Go programs, a line represents the end of a statement. Each statement does not need to end with a semicolon `;` like in other languages of the C family, as this work is automatically done by the Go compiler. If you intend to write multiple statements on the same line, they must be separated by `;` manually, but this practice is not encouraged in actual development. Here are two statements: fmt.Println("Hello, World!") fmt.Println(": .com") * * * ## Comments Comments are not compiled, and every package should have relevant comments. Single-line comments are the most common form of comment. You can use single-line comments starting with `//` anywhere. Multi-line comments, also known as block comments, start with `/*` and end with `*/`. For example: // Single-line comment/* Author by I am a multi-line comment */ * * * ## Identifiers Identifiers are used to name variables, types, and other program entities. An identifier is actually a sequence of one or more letters (A~Z and a~z), digits (0~9), and underscores `_`, but the first character must be a letter or an underscore, not a digit. Here are some valid identifiers: mahesh kumar abc move_name a_123 myname50 _temp j a23b9 retVal Here are some invalid identifiers: * 1ab (starts with a digit) * case (a keyword in Go) * a+b (operators are not allowed) * * * ## String Concatenation String concatenation in Go can be achieved using the `+` operator: ## Example package main import"fmt" func main(){ fmt.Println("Google"+"") } The output of the above example is: GoogleTutorial * * * ## Keywords The following is a list of 25 keywords or reserved words used in Go code: break default func interface select case defer go map struct chan else goto package switch const fallthrough if range type continue for import return var In addition to the keywords listed above, Go has 36 predefined identifiers: append bool byte cap close complex complex64 complex128 uint16 copy false float32 float64 imag int int8 int16 uint32 int32 int64 iota len make new nil panic uint64 print println real recover string true uint uint8 uintptr A program is generally composed of keywords, constants, variables, operators, types, and functions. The following delimiters may be used in a program: parentheses `()`, square brackets `[]`, and curly braces `{}`. The following punctuation marks may be used in a program: `.`, `,`, `;`, `:`, and `…`. * * * ## Whitespace in Go In Go, whitespace is typically used to separate identifiers, keywords, operators, and expressions to improve code readability. Variable declarations in Go must be separated by whitespace, for example: var x intconst Pi float64 = 3.14159265358979323846 Using spaces between operators and operands makes the program easier to read: Without spaces: fruit=apples+oranges; Adding spaces between variables and operators makes the program look more aesthetically pleasing, for example: fruit = apples + oranges; Use spaces between keywords and expressions. For example: if x > 0 { // do something} When calling a function, use a space between the function name and the left parenthesis, and also use spaces between parameters. For example: result := add(2, 3) * * * ## Formatted Strings In Go, `fmt.Sprintf` or `fmt.Printf` can be used to format strings and assign them to new strings: * **Sprintf** generates a formatted string based on the formatting parameters and returns that string. * **Printf** generates a formatted string based on the formatting parameters and writes it to standard output. ## Sprintf Example package main import( "fmt" ) func main(){ // %d represents an integer, %s represents a string var stockcode=123 var enddate="2020-12-31" var url="Code=%d&endDate=%s" var target_url=fmt.Sprintf(url,stockcode,enddate) fmt.Println(target_url) } The output is: Code=123&endDate=2020-12-31 ## Printf Example package main import( "fmt" ) func main(){ // %d represents an integer, %s represents a string var stockcode=123 var enddate="2020-12-31" var url="Code=%d&endDate=%s" fmt.Printf(url,stockcode,enddate) } The output is: Code=123&endDate=2020-12-31 For more content, see:
← Go Switch StatementGo If Else Statement β†’