• Rust中如何引入模块

    rust

    很多时候,我们写的代码需要按模块组织,因为我们无法将大量的代码都写在一个文件上,那样不容易维护,在Rust中我们声明一个模块非常简单,文件名就是模块名,所以创建一个模块就相当于创建一个文件。使用mod关键字引入模块,以下例子,我们创建一个math模块,并在main中引用

    math.rs

    pub fn sum(a: u32, b: u32) -> u32 {
      return a + b;
    }
    

    main.rs

    ...

    READ ALL

  • Angular8错误 ngModel isn't a known property

    Angular8

    很早之前,我和我的团队就已经使用Angular1做项目,在当时来说它的确解决了很多问题。然而Angular2与Angular1差别太大,Angular2等于憋了一个超级大招,可惜没人接招。于是大部分开始转去使用React、Vue或其它的框架了。

    最近,我又开始使用Angular8开始做项目,但在一个Radio组件上绑定ngModel属性时,直接报错

    Console output

    ERROR Error: Uncaught (in promise): Error: Template parse errors:
    Can't bind to 'ngModel' since it isn't a known property of 'mat-radio-group'.
    1. If 'mat-radio-group' is an Angular component and it has 'ngModel' input, then verify that it is part of this module.
    2. If 'mat-radio-group' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("rank-radio-group-label">对比类型,</label>
        <mat-radio-group aria-labelledby="rank-radio-group-label" [ERROR ->][(ngModel)]="selectedType" (change)="render()">
          <mat-radio-button class="rank-type" [disabled]="): ng:///PackageRankModule/NpmComponent.html@7:62
    Error: Template parse errors:
    Can't bind to 'ngModel' since it isn't a known property of 'mat-radio-group'.
    1. If 'mat-radio-group' is an Angular component and it has 'ngModel' input, then verify that it is part of this module.
    2. If 'mat-radio-group' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("rank-radio-group-label">对比类型,</label>
        <mat-radio-group aria-labelledby="rank-radio-group-label" [ERROR ->][(ngModel)]="selectedType" (change)="render()">
          <mat-radio-button class="rank-type" [disabled]="): ng:///PackageRankModule/NpmComponent.html@7:62
    

    ...

    READ ALL

  • Rust中JSON如何解析转换到struct

    rust json

    写Rust有一年多了,我是非常喜欢严谨的语言,就连Rust的Logo看上去都很工业化,具有严谨性。我愿意付出相对多写代码的成本去换取更稳定,更少Runtime错误。写代码到今天已经有将近十年了,一直想选一款编译型语言,之前有用过Golang,但仍然不太满意的是编译后的语言仍然在运行时出现空指针错误,而Rust是我见过跟众多语言别具一格的做法,没有null类型。是的,你没有看错,在Rust里没有null类型。加之在Rust编译器严谨的模式下,你很难写出在Runtime时有空指针的错误。

    ...

    READ ALL

  • Java编译错误 code too large

    thinking man

    背景

    通常公司里做国际化翻译管理的时候都会搭建一个Web应用去编辑管理翻译内容,最后在使用程序生成相应的语言包,我们也一样这么做。原来是生成JavaScript语言包,现在也要使用同样的数据生成Java语言包给Java项目使用。这样做有个好处就是所有的翻译资源统一管理,然后通过读取统一的数据源生成相应的语言包给开发使用,确保它们的翻译内容无论在那个程序语言里都是通用的。

    想法

    所有的翻译内容最终会生成一个i18n.json文件,然后JavaScript和Java去解析这个json文件即可,所以i18n.json相当于一个翻译内容的文件数据库,它的内容结果如下:

    ...

    READ ALL

  • Golang如何读取JSON文件

    Golang

    读取文件

    首先要能读取文件内容,读取文件内容可以使用os.Open()打开一个文件,然后再使用ioutil.ReadAll()读取文件的内容。这里要注意一下,ioutil.ReadAll()读取到的是字节,也就是[]bytes数据类型。如果你需要把字节转换成字符可以在使用string()函数转成字符。如这个例子,读取user.json文件并打印出内容

    user.json

    {
      "first_name": "Nicholas",
      "last_name": "Lee",
      "blog_url": "https://www.qttc.net",
      "blog_name": "琼台博客"
    }
    

    ...

    READ ALL

  • JavaScript中为什么async/await在forEach里不工作

    Node await async

    写JavaScript最头疼的就是回调函数,反人类的设计,虽然IO异步机制很牛逼,于是各种轮子开始造起来,目的是让你尽量以同步IO的方式编写异步IO程序,这样的代码易于维护,并且还能保持IO异步的特性。

    其中,async/await就是其中一个轮子,这货目前看起来还是比较受欢迎的,我也经常在使用。今天要正好也要用它发现不工作了,大致代码如下:

    app.js

    const numbers = [33, 41, 57];
    
    let sum = 0;
    
    const sumFunction = (a, b) => {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve(a + b);
        }, 1000);
      });
    };
    
    numbers.forEach(async (number) => {
      sum = await sumFunction(sum, number);
    });
    
    console.log(sum);
    

    ...

    READ ALL

  • Kibana 请求超时错误 Request Timeout after 30000ms

    Kibana

    在Kubernetes上为了更好的管理查看日志,通常都使用ELK(Elasticsearch Logstash Kibana)或者EFK(Elasticsearch Fluentd Kibana)方案统一收集并在Kibana上查询日志。可能是在我搭建EFK的时候没有给Elasticsearch足够的资源,导致Kibana在查询Elasticsearch时没有及时得到响应,此时Kibana会自动停止该请求,并提示用户请求超时

    ...

    READ ALL

  • Kubernetes如何在Deployment中更新ConfigMap

    Clock

    前两天我写了一篇文章《Kubernetes如何修改从文件创建的ConfigMap内容》介绍了如何更新ConfigMap,今天我要说的是ConfigMap更新之后如何让已经在使用Deployment方式部署Pods里的ConfigMap生效,因为当你变更ConfigMap之后已经在运行的Pods是不会自动更新最新的ConfigMap的,需要手动处理一下。

    ...

    READ ALL

  • Kubernetes如何修改从文件创建的ConfigMap内容

    Grumpy Cat

    在Kuberenetes中ConfigMap是最常用的功能了,尤其在容器化弹性快速部署的模式下,应用的配置就可以通过ConfigMap很好的管理。ConfigMap有Key和Value配成一对,一个ConfigMap中可以有很多个Key-Value对,创建ConfigMap时,Key-Value信息可以从字面量来,也可以从文件中来

    • kubectl create cm my-config --from-literal=key1=config1 --from-literal=key2=config2 从字面量创建key=value对
    • kubectl create cm my-config --from-file=path/to/bar 从文件中创建key=value对,key则是文件名bar,value就是文件内容

    ...

    READ ALL

  • Nginx使用proxy_redirect替换proxy_pass Response 301/302的Location字段

    nginx

    Nginx通过proxy_pass反向代理请求到任意一个地址,并将Response返回给用户,多数情况下这是没什么问题的,但有一种情况下需要额外处理301/302的Location字段,假设

    • Server: 192.168.1.2: 是内网中的一台服务,在内网环境中大家都直接访问它
    • Nginx: 192.168.1.3: 是一台外网入口的Nginx服务,访问外网IP将直接访问到它,它会反向代理到192.168.1.2

    ...

    READ ALL