Go Language Adventure Thinking notes

Source: Internet
Author: User

Recently contacted Object storage, the international dingding famous Amazon S3 also has the Google Cloud service in the domestic because the firewall and the engine room too far problem, and cannot put into production use. Domestic well-known object storage, everyone knows the seven qiniu, backstage is the Go language Implementation. I always admire the first person who dares to eat crab. So with a vision of the mentality into the door of the go language.
  
first, touching a language begins with the most verbose, but most indispensable, basic grammar and Order. For the touch of these, I generally start from building their own build environment, first download the Go language sdk, domestic: I use the latest 1.7.2, if you want to use the idea of Go plugin programming, you need to use 1.4.2, not with the high Version. After that, download the liteide and encode: we open liteide, create a new example project in a Non-gopath directory (we spit in the trough liteide, he is not a very rigorous, very useful, very complete ide, for the habit of idea lazy programmer, may not be used To. however, the return to the original is also very good, named Gotest.
  
The project automatically generates directories and Code. We can only keep main.go. The go language has his intelligent side, first of all two principles: for each project, you need to configure Gopath. In order to build this project, the project root directory needs to be added to the gopath, otherwise the package within the project cannot be found.
  
Go based on the project directory structure of the automatic construction, here is the basic principle is: need a main portal, your own new application or a code package is a new folder in the SRC directory, the folder name is generally the name of the code package, of course, also allow multi-level directory, for example, The new directory under SRC $gopath/ Src/github.com/hashzhang/test so this package path is "github.com/hashzhang/test", the package name is the last directory test variable definition The Go language takes into account freedom and rigor: first, for the definition of variables, We have three different ways, very free. then, the useless elements are not passed at compile Time. For example: useless package introduction, useless variable definition, these will be at compile time Error.
  
Below we write a small program: we create a new pkg and src two folders in the project root directory, src directory store Our source code, pkg is to go compiler, will generate compiled files, in fact, is compiled good library files. The root directory remains in the SRC directory under the new fundamental folder, and a new variables.go is created in the fundamental folder.
  
Write the source code (here first put forward a little go language without semicolon division, enter can): if the package is useless, it will be in the compile times wrong, here used the standard output function, the module to export functions, must be Capitalized.
  
If the variable is not used, it will be in the Compile-time error, initialized to the default value of 0 Note lowercase s, initialized to the default value "" array, initialized to the default value of the dynamic array, the array slice, initialized to the default value [] is equivalent to the struct in the C language, initialized to the default value} pointer, initialized to a Value is an int, which is initialized to the default value defined as a function, which specifies the input output, initialized to the default value automatically judging by the initial value type third initialization} general idea exchange, using A+B-A and a+b-b to achieve the purpose of exchange
  
} language exchange, because functions can return multiple values and support multi-assignment syntax similar to A,b=b,a
  
}, do not capitalize the first letter for functions that do not need to be exported in the Module.
  
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 Www.ymzxrj.com 561234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 We conclude by: 1 There are 3 ways to define or initialize a variable: 1231232. the variable type is very free, there are c-language constructs, and there are function type variables like Javascript.
  
3. Variable assignment is very free, for other languages, variable exchange has two Ways: one is the intermediate variable, the other is the use of a+b-a and a+b-b to achieve the purpose of Exchange. The go language supports multiple assignments, the exchange of conventional ideas, and the use of a+b-a and a+b-b to achieve Exchange Purposes.
  
} language exchange, because functions can return multiple values and support multi-assignment syntax similar to A,b=b,a
  
}1234567891011121314151617181920211234567891011121www.sratchina.com314151617181920214. The above multiple assignments, also from the Go language function can return multiple values 。
  
, do not capitalize the first letter for functions that do not need to be exported in the Module.
  
Both the VAR keyword and the import keyword for the}1231235.go language support multiple definitions in parentheses, for example: var ()
  
123412346. the function,function name that is visible outside the package must be capitalized (that is, the method that uses the initial capitalization defines the function that can be called externally visible)
  
Here we can invoke the initialization (www.lafei333.cn), SwapDisplay1 (www.feiyunfan.cn), SwapDisplay2 (www.yunfanfei.cn) method, And the Swap method is not Allowed.
  
Written in Main.go www.huafanyun.cn:)
  
, the function you want to export in the module must be capitalized in the first letter.
  
, the function to be exported in the module must be capitalized first, and the function to be exported in the module must be capitalized in the first letter} 12345678910111213141234567891011121314 compiling run: 1234567891011121314151617181912345678910111213141516171819 generated file: write a picture here as we said before , the. a library file is automatically generated under the PKG directory. An EXE executable file with the same name as the project is also generated under the root directory.
  
Constant definition The literal of the go language is untyped in the fundamental folder new Constants.go: if the package does not work, it will be wrong in the compile times, where the standard output function is used instead of specifying whether it is a float or a double, with the end plus f, the literal constants are untyped, and the actual It is not necessary to specify whether it is a float or double as in other languages, the end with f, the literal constant is untyped, and here is the fact that no type is specified, which automatically matches the floating-point type constant that can accommodate the Variable.
  
Three predefined constants, iota are incremented before the next const, but must be incremented on different assignment statements.
  
)
  
Must be in a different assignment statement to Increment.
  
)
  
}12345678910111213141516171819202122232425262728293031321234567891011121314151617181920212223242526272829303132 summarizes the following Points: 1 . The literal of the go language is untyped, and for floating-point types, you don't have to specify float or double as in other languages, end with f, literal constants are untyped, don't specify float or double as in other languages, end with f, literal constants are untyped, Here is actually not the same as other languages specify whether float or double, the end of the plus f, literal constants are untyped, here is actually 12312www.yunfeizao.cn 32. when you define a constant, you do not specify a type. will automatically match the floating-point type constant 3 that can hold the Variable. three predefined constants (true,false,iota), Iota are incremented before the next const, but must be incremented on different assignment statements.
  
Three predefined constants, iota are incremented before the next const, but must be incremented on different assignment statements.
  
)
  
Must be in a different assignment statement to Increment.
  
)
  
The 123456789123456789 type Boolean type is initialized to type bool, and the default is an expression that can be assigned, with parentheses}1234567812345678 summary: 1. Boolean type initialization, default is 2. The Boolean type does not support other assignments or casts, such as assigning a value of 1

Go Language Adventure Thinking notes

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.