Go

nodejs-go内存占比

月盾

同一台服务器上部署了两个功能差不多的服务,但是内存占比差距有点大。 nodejs-go内存占比 go占14.7M nodejs占122.2M

go语言实现继承,重写

月盾

以实际遇到过得情况为例,用户的数据结构中有类型为日期类型time.Time的createdAt属性,经过反复的格式化处理,在页面上输出的还是2017-05-31 06:49:09 +0800 CST这种格式,所以猜想日期类型是不能直接输出2017-05-31 06:49:09格式的,只能输出格式化后的字符串类型。于是利用go的继承将User的数据结构继承都UserPojo里,再单独对createdAt进行修改,重写为string类型。

package main

import "fmt"
import "time"

type User struct {
    name string
    age int
    createdAt time.Time
}

type UserPojo struct {
    User
    createdAt string
}

func (user *User) getName(){
    fmt.Println("获取用户名:", user.name)
}

func main()  {
    user := new(User)
    user.name="张三"
    user.age=26
    user.createdAt=time.Now()
    fmt.Println("user.createdAt",user.createdAt)

    userpj := new(UserPojo)
    userpj.User = *user
    userpj.createdAt = user.createdAt.Format("2006-01-02 15:04:05")
    fmt.Println("userpj.createdAt",userpj.createdAt)
}

//输出
//user.createdAt 2017-06-17 10:39:29.5294 +0800 CST
//userpj.createdAt 2017-06-17 10:39:29

在go的继承中有一点需要注意,使用结构体struct字面量赋值会出现找不到属性的问题:

https://hopefully-img.yuedun.wang/go_extends_20180328182436.pn

# command-line-arguments
.\test.go:17:7: unknown field 'Name' in struct literal of type Cat
.\test.go:18:8: unknown field 'Color' in struct literal of type Cat

换一种方式还是有问题:

beego post请求获取request body参数

月盾

为了获取json类型的参数煞费苦心,差点不再爱了。

前端请求代码:

$.ajax({
    url: "/user",
    type: "post",
    contentType: 'application/json',
    data: JSON.stringify({username:"张三",mobile:"13265478965"}),
    //这才是最重要的地方,必须用JSON.stringify序列化成字符串,
    //直接使用对象死活都接收不到,至于大小写并不影响,只要写对了就行
    dataType: "json"
}).done(function(res) {
    if(res.result){
        alert("成功")
    }
});

需要传输json类型数据,同时数据需要使用JSON.stringify序列化。

后端接收代码:

func (c *UserController) Post() {
    var form struct {
		Username string `json:"username"`
		Mobile   string `json:"mobile"`
	}
	c.BindJSON(&form)
    user := &User{Username: form.UserName, Mobile: form.Mobile}
    err := user.AddUser()//这是添加用户函数
    if nil != err {
        c.Data["json"] = map[string]interface{}{"result": false, "msg": err}
    } else {
        c.Data["json"] = map[string]interface{}{"result": true, "msg": "新增成功"}
    }
    c.ServeJSON()
}

可以使用beego提供的BindJSON函数解析json数据。

特别注意: 如果数据类型不匹配也会造成空数据结果。 例如:前端参数是age: "23",model中age int,也会获取不到数据。

Go语言学习笔记

月盾

近些年出现不少新的开发语言,比如:

Rust是Mozilla开发的注重安全、性能和并发性的编程语言。

Go语言是谷歌2009发布的第二款开源编程语言。专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全、支持并行进程。

D语言最初由Digital Mars公司就职的Walter Bright于2001年发布,意图改进C++语言。目前最新D语言被简称为D2。最主要的D语言的实现是DMD。

Node.js是一个基于Chrome JavaScript运行时建立的平台, 用于方便地搭建响应速度快、易于扩展的网络应用。Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效,非常适合在分布式设备上运行的数据密集型的实时应用。

等等……

对于这些新语言,总有不少槽点被开发者吐槽。很多人都会进行对比,褒贬。但是我还是首先选择了Nodejs,简单快速上手,目前完全用于生产环境,它的快速开发让我很满意。ES6标准的定稿更让我肯定nodejs将会有更好的发展。

最近又学习了go语言,在学一门语言前总会迟疑值不值得学习,然后看到网上各种讨论。但我觉得还是值得学习,且不说性能有没有C++好,光凭开发效率就值得我学了,现代计算机性能都有很大的提升,如果不是开发底层系统,而是开发软件服务端系统那么它的有点足够了。

原生http请求获取参数

  • 获取URL参数

  • url:localhost:8081/?startDate=2017-11-22 var query = req.URL.Query() Println(query["startDate"])

  • go返回json数据

type ResObj struct {
  Code int
  Data map[string]string 
}
//或
//type ResObj struct {
//  Code int `json:"code"`
//  Data map[string]string `json:"data"`
//}
//首先要保证ResObj的属性首字母大写,否则访问不到,其次`json:"code"`不是必须,只不过加了什么样返回的json就是什么样
//{"Code":0,"Data":{"a":"aaaaaa"}}和{"code":0,"data":{"a":"aaaaaa"}}这样的区别
func helloHandler(res http.ResponseWriter, req *http.Request) {
  m := make(map[string]string)
  m["a"] = "aaaaaa"
  ress := &ResObj{
    Code: 0,
    Data: m}
  byt, _ := json.Marshal(ress)
  var resb ResObj
  json.Unmarshal(byt, &resb)
  fmt.Println(resb)
  res.Write(byt)
  //为什么Write参数要是byte数组?就不能像Nodejs直接是个对象。其实这是偏于底层了,Nodejs最后返回的也是byte,请看下文
}

nodejs的write函数:

response.write(chunk[, encoding][, callback])

Added in: v0.1.29