例子1
package main
import (
"awesomeProject/logger"
"fmt"
"github.com/opencontainers/runtime-tools/filepath"
"os"
)
type Goods struct {
ID uint
MonTs string
}
func Mkfile() {
filepaht := filepath.Join(os.TempDir(), "nginx")
if _, err := os.Stat(filepaht); os.IsNotExist(err) {
err = os.MkdirAll(filepaht, 0644)
if err != nil {
logger.DefaultLogger.Errorf("%v", err)
}
}
}
func NewGoods(id uint) *Goods {
return &Goods{ID: id}
}
func (g *Goods) SetMontsAdd(m string) *Goods {
g.MonTs = m
return g
}
func main() {
g := NewGoods(100).SetMontsAdd("TE")
fmt.Println(g) //{100 TE}
}
例子2
package main
import (
"awesomeProject/logger"
"fmt"
"github.com/opencontainers/runtime-tools/filepath"
"os"
)
type Goods struct {
ID uint
MonTs string
}
func Mkfile() {
filepaht := filepath.Join(os.TempDir(), "nginx")
if _, err := os.Stat(filepaht); os.IsNotExist(err) {
err = os.MkdirAll(filepaht, 0644)
if err != nil {
logger.DefaultLogger.Errorf("%v", err)
}
}
}
func (g *Goods) SetId(id uint) *Goods {
g.ID = id
return g
}
func (g *Goods) SetMonts(m string) *Goods {
g.MonTs = m
fmt.Printf("%T\n", g)
return g
}
func main() {
var g Goods //或者var g = &Goods{}
g.SetId(100).SetMonts("test")
fmt.Println(g) //{100 test}
}
例子3
package main
import (
"awesomeProject/logger"
"fmt"
"github.com/opencontainers/runtime-tools/filepath"
"os"
)
type Goods struct {
ID uint
MonTs string
}
func Mkfile() {
filepaht := filepath.Join(os.TempDir(), "nginx")
if _, err := os.Stat(filepaht); os.IsNotExist(err) {
err = os.MkdirAll(filepaht, 0644)
if err != nil {
logger.DefaultLogger.Errorf("%v", err)
}
}
}
func (g *Goods) SetId(id uint) uint {
g.ID = id
return g.ID
}
func (g *Goods) SetMonts(m string) string {
g.MonTs = m
return g.MonTs
}
func main() {
var g *Goods //(改为var g Goods 或者var g = &Goods{})
g.SetId(100)
g.SetMonts("test")
fmt.Println(g)
} // 用var g *Goods 报错 runtime error: invalid memory address or nil pointer dereference