Browse Source

Go与lua架构

fengchun_yuan 2 years ago
parent
commit
7a66020eef
2 changed files with 168 additions and 0 deletions
  1. 116 0
      Go语言101/system.md
  2. 52 0
      note/entngx架构.md

+ 116 - 0
Go语言101/system.md

@@ -215,6 +215,42 @@ len(map类型) = 0
 
 append内置函数,需要理解。
 
+### 对于map类型m;
+
+```go
+m[k] = e //添加 k,e 键值对
+delete(m, k) //删除m的k键值对;即使m为nil,也不会产生panic
+```
+
+### 对于切片类型;
+
+**切片类型的值传递要慎用**
+
+```go
+1| package main
+2|
+3| import "fmt"
+4|
+5| func main() {
+6| s0 := []int{2, 3, 5}
+7| fmt.Println(s0, cap(s0)) // [2 3 5] 3
+8| s1 := append(s0, 7) // 添加一个元素
+9| fmt.Println(s1, cap(s1)) // [2 3 5 7] 6
+10| s2 := append(s1, 11, 13) // 添加两个元素
+11| fmt.Println(s2, cap(s2)) // [2 3 5 7 11 13] 6
+12| s3 := append(s0) // <=> s3 := s0
+13| fmt.Println(s3, cap(s3)) // [2 3 5] 3
+14| s4 := append(s0, s0...) // 以s0为基础添加s0中所有的元素
+15| fmt.Println(s4, cap(s4)) // [2 3 5 2 3 5] 6
+16|
+17| s0[0], s1[0] = 99, 789
+18| fmt.Println(s2[0], s3[0], s4[0]) // 789 99 2
+19| }
+
+```
+
+
+
 ## make函数
 
 可以用make创建切片和映射,数组不行
@@ -337,4 +373,84 @@ append内置函数,需要理解。
 4| }
 
 ```
+
+## 上述各种容器操作内部都未同步
+
+在没有协程修改一个容器值和它的元素的时候,多个协程并发读取此容器值和它的元素是安全的;但是并发修改同一个容器值是不安全的。不使用并发同步技术而并发修改同一个容器值将会造成数据竞争。
+
+# 字符串
+
+## 内部结构
+
+```go
+1| type _string struct {
+2| elements *byte // 引用着底层的字节
+3| len int // 字符串中的字节数
+4| }
+```
+
+- 使用内置函数len获取一个字符串的长度(此字符串中存储的字节数)
+- 使用容器元素索引语法aString[i]获取aString中的第i个字节。表达式aString[i]是不可寻址的。即aString[i]不可被修改
+- 使用子切片语法aString[start:end]获取aString的一个子字符串。start和end均为aString中存储的字节的下标
+
+**对于标准编译器来说,字符串的赋值完成后,此复制的目标值和源值将和基础字符串aString共享一部分底层字节**
+
+**当一个字符串被转换为一个字节切片时,结构切片中的底层字节序列是此字符串中存储的字节序列的一份深复制;当一个字节切片被转换为一个字符串时,此字节切片中的字节序列也将被深复制到结果字符串中。字符串和字符接片是不能共享底层字节序列的**
+
+## 字符串、切片类型转换
+
+```go
+1| package main
+2|
+3| import (
+4| "bytes"
+5| "unicode/utf8"
+6| )
+7|
+8| func Runes2Bytes(rs []rune) []byte {
+9| n := 0
+10| for _, r := range rs {
+11| n += utf8.RuneLen(r)
+12| }
+13| n, bs := 0, make([]byte, n)
+14| for _, r := range rs {
+15| n += utf8.EncodeRune(bs[n:], r)
+16| }
+17| return bs
+18| }
+19|
+20| func main() {
+21| s := "颜色感染是一个有趣的游戏。"
+22| bs := []byte(s) // string -> []byte
+23| s = string(bs) // []byte -> string
+24| rs := []rune(s) // string -> []rune
+25| s = string(rs) // []rune -> string
+26| rs = bytes.Runes(bs) // []byte -> []rune
+27| bs = Runes2Bytes(rs) // []rune -> []byte
+28| }
+```
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 

+ 52 - 0
note/entngx架构.md

@@ -0,0 +1,52 @@
+# entngx项目
+
+## 复刻用例
+
+ssh root@47.106.12.245
+Yfc256631!
+
+1. 阿里云 ubuntu18.04  x86_64
+2. 安装配置教程http://openresty.org/cn/getting-started.html
+
+### 一些笔记
+
+```sh
+#!/bin/sh
+/usr/local/openresty/nginx/sbin/nginx  -c /root/about_nginx/work/conf/nginx.conf
+#启动是work目录下的nginx.conf文件
+#教程:http://openresty.org/cn/getting-started.html
+#PATH的设置
+#export PATH=/usr/local/openresty/nginx/sbin:$PATH
+```
+
+```sh
+openresty已经搭建,具体参考 http://openresty.org/cn/linux-packages.html#ubuntu | http://openresty.org/cn/installation.html
+nginx的默认目录是/usr/local/openresty/nginx
+这里配置了work测试目录。教程链接http://openresty.org/cn/getting-started.html
+
+killall nginx #杀死所有nginx进程
+
+netstat -tlnp #查看网络状态
+
+ps -ef|grep nginx #查看nginx有关进程
+
+创建的.sh文件,是用于指定启动nginx.conf配置文件
+
+/usr/local/openresty/bin/resty test.lua 指定运行文件
+
+/usr/local/openresty/nginx/conf/?.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/site/lualib/?.lua;;
+#默认lua包位置
+/usr/local/openresty/lualib/?.so;/usr/local/openresty/nginx/lua/lib/lua/5.1/?.so;;
+#可能是nginx的包位置,openresty没有
+
+#运行test.lua
+/usr/local/openresty/bin/resty test.lua
+
+lua的库可以是.lua,也可以是.so
+```
+
+### 一些坑
+
+1. nginx 配好了,外网ip无法访问。
+   解决方案:阿里云安全组怎么开放全部端口
+