Go Language Core Technology (Vol. 1): basic Unit 1

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

I. Naming Chapter

1.Go functions, variables, constants, custom types, Packages (package) are named according to the following rules:

1) The first character can be any Unicode character or underline

2) The remaining characters can be Unicode characters, underscores, numbers

3) Unlimited character length


2.Go only 25 keywords

Break default Func interface Selectcase defer go map Structchan Else Goto Package Switchconst Fallthrough if range typecontinue F or import return var

3.Go plus 37 reserved words

Constants: true false iota nil

types:  int int8 int16 int32 int64
  uint uint8 uint16 uint32 uint64 uintptr
  float32 float64 complex128 complex64
bool byte rune string error
functions:  make len Cap New append copy close delete
  complex Real imag
panic recover


4. Visibility

1) declared inside the function, is the local value of the function, similar to the private

2) declared outside the function, is a global value that is visible to the current package (all. Go files within the package are visible), similar to protect

3) declaring outside the function with the first letter capitalized is a global value that is visible to all packages, similar to public


5. Naming style

For local variables, go prefers to use a short naming method, such as I in A For loop. The larger the scope of the variable, the more meaningful the name of the variable, such as Ringcache.

The go variable suggests using the Hump naming method, such as Parserequest. Abbreviated phrases are either all uppercase or all lowercase. such as Htmlescape,htmlescape.


Two. Statement

There are four main ways of declaring this: Var, const, type, func (further articles are detailed).

The go program is saved in multiple. Go files, the first line of the file is the package XXX declaration, which is used to indicate which packages the file belongs to, and then it declares the import declaration, and then it is irrelevant to suck type, variable, constant, function declaration. For example, the following program declares a constant, a function, some variables

Package Mainimport "FMT" Const BOILINGF = 212.0func Main () {    var f = boilingf    var c = (f-32) * 5/9    FMT. Printf ("boiling point =%g°f or%g°c\n", F, C)    //output:    //boiling point = 212°f or 100°c}

The constant BOILLINGF is a variable that is visible within the package (just like the function main), and F and C are local variables of the main function and are only visible to the main function. If a variable is visible within the package, not only the current. Go file is visible and all the. Go files within the package are visible.

A function declaration contains a function name, a list of parameters, a return value list, and a function body. If the function does not return a value, the returned list can be omitted. The function executes from the first statement until the return statement is executed or the last statement of the function is executed. In the following code, the main function calls the FTOC function two times, declaring two local constants

Package Mainimport "FMT" Func Main () {    const FREEZINGF, BOILINGF = 32.0, 212.0    fmt. Printf ("%g°f =%g°c\n", FREEZINGF, FToC (FREEZINGF))//"32°F = 0°c"    fmt. Printf ("%g°f =%g°c\n", BOILINGF, FToC (BOILINGF))   //"212°f = 100°c"}func FToC (F float64) float64 {    return (f-32 ) * 5/9}




Welcome to join the Go language core technology QQ group 894864, inside a lot of enthusiastic God Oh!


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.