Golang Study Notes

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

Golang Study Notes

Basic syntax

Keywords (key word) && variables

  • The
  • Keyword
    Golang has only 25 keywords, as follows:

     import  type  return  const  if  else  for  range  break  select  default< /span> goto  switch  fallthrough  func  interface  case  chan  continue  defer  go  map struct  package   
  • Variable declaration && Initialization && Assignment
    The Golang variable declaration way differs from the C + + system language Obviously, to the Golang this kind of declaration way has discussed why the Go language put the type behind?
    Golang introduces the var keyword, and type information is placed after the variable name.
    "Go
    var n int//define an int variable
    var f float32
    var str string//define a string variable
    var arr [10]int//array
    var m map[int]int//Map
    var p *int//pointer

var n int = 10//define and initialize
var n = 10
N: = 10//Effect sample, can be automatically pushed to n type

n = 100//Assignment
N2: = N//define and initialize

......

由上面示例可以看出,golang语句不需要分号作结束符,而且定义一个变量是被初始化的一般默认值是`0`,而指针是`nil`,`string`是`""`。

Package Main

Import "FMT"

Func Main () {
var n int
Fmt. PRINTLN ("int:", N)
var f float32
Fmt. Println ("float:", f)
var str string
Fmt. Println ("string:", str)
var arr [10]int
Fmt. Printf ("array:")
For I: = 0; I < Len (arr); i++ {
Fmt. Printf ("%d", Arr[i])
}
Fmt. Println ()
var p int
Fmt. PRINTLN ("
int:", p)
}
/
Output
int:0
float:0
String
array:0 0 0 0 0 0 0 0 0 0
Int:
*/
```

Types (Type)

Built-in base type in Golang

    • Boolean
    • int8, Byte, int16, int, uint, uintptr
    • float32, float64
    • Complex64, complex128
    • String
    • Rune
    • Error

Composite type

    • Arrays (Array)
    • Dictionary (map)
    • Slicing (slice)
    • struct (struct)
    • Channel (Chan)
    • Interface (interface)
    • Pointer (pointer)
    • References (Reference)

Process controls (flow control)

    • Conditional statements
      If Else
    • SELECT statement
      Switch Case Select
    • Looping statements
      For range
    • Jump statement
      Goto

Functions (function) && packages (package)

    • function definition
      "Go
      Func Func_name () {

}
```

    • Function call
    • Indeterminate parameters
    • Multiple return values
    • Anonymous functions && Closures
    • Package Management

Object oriented

    • Methods (method)
    • Anonymous fields
    • Interface (interface)
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.