K8s source code and style specifications

### K8s source code and style specifications

I. Project Overview

  • The project is mainly based on conduct Kubernetes clusters for extensions for the open source project. Requirements for extension development based on existing clusters and Kubernetes Prometheus monitoring system.
  • K8s was developed by Google, go and use language development.

Second, the source code ready

1. System environment:
  • Operating System: We use Linux as k8s source code analysis and debugging environment, fedora, centos, ubuntu will do, here I use fedora;
  • golang Related:
GOROOT=/usr/local/lib/golang
GOPATH=/root/go
go version go1.10.3 linux/amd64
2. Download the source code
mkdir -p /root/go/src/k8s.io
cd /root/go/src/k8s.io/
git clone https://github.com/kubernetes/kubernetes.git
  • Catalog display:

  • The main catalog:

Directory name Features
cmd Each code entry component (main function)
pkg Specific functions of the respective components to achieve
staging It has been sub-library project
vendor rely
3.IDE
  • JetBrains mail application through the school education account, look at the code by Goland:

Three, go language features and naming conventions

1.go Language Features

GO key features of the language include the following aspects:

  • Concurrent with the coroutine
  • Based message passing communication
  • Rich and useful built-in data types
  • Multi-function return values
  • defer mechanism
  • Reflection (the reflect)
  • High-performance HTTP Server
  • Engineering Management
  • Programming Specification
2. relevant norms
  • package name
    to keep package names and directories consistent, try to take a meaningful package name, a brief, meaningful, and as far as possible the standard library do not conflict.

  • import specification
    import in the case of multi-line, automated tools will automatically help you format, but here we still import some specifications about the standard, if you introduce a package in a file inside, it is recommended that the following format:

import (
    "fmt"
)

If your package introduces three types of packages, the standard library package, the internal package, third-party packages, recommend the following ways to organize your package:

import (
    "strings"

    "myproject/models"
    "myproject/controller"

    "github.com/mysql"
)   

Sequential introduction of packages of different types using a space separation, the first real standard library, the second item is the package, the third a third party package. Do not use relative paths in the project introduced package:

// 这是不好的导入
import “../pkg”

// 这是正确的做法
import “github.com/tx23/pkg”
  • Variables declared
    variable name using standard hump, do not use _ to name the variable name, declare multiple variables together
    affirm the need to use var outside the function, do not use: = easy to step on the issue of the scope of variables.
var (
    Found bool
    count int
)
  • Since the definition of the type of string circulatory problems
    if a custom type defines String method, then during printing will produce some bug hidden.
type MyInt int
func (m MyInt) String() string { 
    return fmt.Sprint(m)   //BUG:死循环
}

func(m MyInt) String() string { 
    return fmt.Sprint(int(m))   //这是安全的,因为我们内部进行了类型转换
}
  • Avoid returning named parameter
    if your function is very short, less than 10 lines of code, you can use, or directly use types, because if you use named variables can easily lead to hidden bug.
func Foo(a int, b int) (string, ok){

}

Of course, if there are multiple parameters of the same type of return, so named parameter may be more clear.

func (f *Foo) Location() (float64, float64, error)

  • Error handling
    Error handling of the principle that they can not discard any calls that return err, do not discard the use of _ must all deal with. Receive an error, or return to err, or it is not to panic, or use the log recorded information of error not to use capital letters, try to keep your mistakes brief, but enough to express your mistakes mean.

  • Note Closure call
    in a loop goroutine function or method call, be sure to use a variable display calls which do not invoke the closure function of cycle parameters

fori:=0;i<limit;i++{
    go func(){ DoSomething(i) }() //错误的做法
    go func(i int){ DoSomething(i) }(i)//正确的做法
}
  • Disable panic in the logic processing
    only when the situation is really not panic run using, for example, a file can not be opened in the main package, the database can not connect cause the program to not function properly, but for other external interface of the package can not have panic, only in package in the adoption. It is strongly recommended to use log.Fatal in the main pack to record the error, so that you can log to end the program.

  • struct specification
    struct declaration and initialization of multi-line format:
    defined as follows:

type User struct{
    Username  string
    Email     string
}

Initialized as follows:

u := User{
    Username: "astaxie",
    Email:    "[email protected]",
}
  • recieved value type or a pointer type
    in the end is the use of a pointer value type or types of reference to the following main principles:
func(w Win) Tally(playerPlayer)int    //w不会有任何改变 
func(w *Win) Tally(playerPlayer)int    //w会改变数据

For more, please refer to:
https://code.google.com/p/go-wiki/wiki/CodeReviewComments#Receiver_Type

  • Struct with a mutex must be a pointer receivers
    if you defined struct with a mutex, then your receivers must be a pointer

Reference:
https://code.google.com/p/go-wiki/wiki/CodeReviewComments
http://golang.org/doc/effective_go.html

Guess you like

Origin www.cnblogs.com/tangxin2019/p/11615739.html