go style guide
import ../config
import github.com/voidint/gbb/config
func run() (n int, err error) {
// ...
return
}
func run() (n int, err error) {
// ...
return n, err
}
`intuint
int```
type Person struct{
Age int
}
type Person struct{
Age uint
}
slice
s := []string{} //
var s []string //
:=
func saySomething() {
var msg = getMsg()
fmt.Println(msg)
}
func saySomething() {
msg := getMsg()
fmt.Println(msg)
}
``
type Student struct{
Name string
}
stu := Student{
Name: "voidint",
}
func main(){
fmt.Printf("student name: %s\n", stu.Name)
}
type Student struct{
name string
}
func NewStudent(name string) *Student{
return &Student{name: name}
}
func (stu Student) Name() string{
return stu.name
}
func main(){
fmt.Printf("student name: %s\n", NewStudent("voidint").Name())
}
``
var student_name string
const LOG_LEVEL = "info"
var studentName string
const LogLevel = "info"
var (
Cpu string
Id int
)
var (
CPU string
id int
)
func add(X, Y int) (Z int) {
return X + Y
}
func add(x, y int) (z int) {
return x + y
}
godoc
func SplitHostPort(hostport string) (string, string, error){
...
}
func SplitHostPort(hostport string) (host, port string, err error){
...
}
package busyBox
package busybox
package utils
package util
package chubby
type ChubbyFile struct{
}
package chubby
type File struct{
}
er
type IRead interface{
}
type Reader interface{
}
// service/network_service.go
package service
// service/network.go
package service
error
_``errorerror
error
scores := map[string]int{
"jim": 8,
"jerry": 7,
"tom": 3,
}
b,_ := json.Marshal(scores) // TODO
fmt.Printf("%s", b)
var (
ErrInvalidIP = errors.New("IP")
ErrInvalidMacAddr = errors.New("Invalid mac address")
)
var (
ErrInvalidIP = errors.New("invalid ip")
ErrInvalidMacAddr = errors.New("invalid mac address")
)
var (
ErrInvalidIP = errors.New("invalid ip!")
)
var (
ErrInvalidIP = errors.New("invalid ip")
)
error``panic
func (repo model.Repo) GetUserByName(name string)(user *model.User){
if name == ""{
panic("user name can't be empty")
}
...
}
var ErrEmptyUserName = errors.New("user name can't be empty")
func (repo model.Repo) GetUserByName(name string)(user *model.User, err error){
if name == ""{
return nil, ErrEmptyUserName
}
...
}
`errorpanic
Must```
package regexp
// MustCompile is like Compile but panics if the expression cannot be parsed.
// It simplifies safe initialization of global variables holding compiled regular
// expressions.
func MustCompile(str string) *Regexp {
regexp, error := Compile(str)
if error != nil {
panic(`regexp: Compile(` + quote(str) + `): ` + error.Error())
}
return regexp
}
```panic`
type Season int
const (
Spring Season = iota
Summer
Fall
Winter
)
func SeasonName(season Season) string {
switch season {
case Spring:
return "Spring"
case Summer:
return "Summer"
case Fall:
return "Fall"
case Winter:
return "Winter"
}
panic("unreachable")
}
error``panic``error
+``fmt.Sprintf``strings.Builder
func (stu Student) String() string {
return stu.Num + " " + stu.Name + " " + stu.Age
}
func (stu Student) String() string {
return fmt.Sprintf("%s %s %d", stu.Num, stu.Name, stu.Age)
}
// OR
func (stu Student) String() string {
var buf bytes.Buffer
buf.WriteString(stu.Num)
buf.WriteString(" ")
buf.WriteString(stu.Name)
buf.WriteString(" ")
buf.WriteString(stu.Age)
return buf.String()
}
receivermap,func,chan
receiverslice,
receiver,
receiver
receiver
``
err := file.Chmod(0664)
if err != nil {
return err
}
if err := file.Chmod(0664); err != nil {
return err
}
return
func gender(female bool) (desc string){
if female {
return "female"
} else {
return "male"
}
}
func gender(female bool) (desc string){
if female {
return "female"
}
return "male"
}
func gender(female bool) (desc string){
if female == true {
return "female"
}
return "male"
}
func gender(female bool) (desc string){
if female{
return "female"
}
return "male"
}
slice
type BigStruct struct{
ID int
}
items := []BigStruct{
{ID: 0},
{ID: 1},
}
for _, item := range items{ // item
fmt.Println(item.ID)
}
type BigStruct struct{
ID int
}
items := []BigStruct{
{ID: 0},
{ID: 1},
}
for i := range items{ // slice
fmt.Println(items[i].ID)
}