转载自: Github Copilot 使用技巧 - 简书

GitHub Copilot是一款由GitHub和OpenAI联合开发的人工智能编程工具,它使用机器学习技术从代码库中学习并为用户生成代码建议。Copilot可以极大地提高编写代码的效率,让开发者更专注于业务逻辑实现,减少了一些繁琐的编码工作。在这篇博客中,我们将介绍一些使用GitHub Copilot的技巧,以帮助你更好地利用这个工具。 -- ChatGPT

1、翻译补全

2、算法生成

3、测试用例

4、结果输出

输出特定范围内全部可能需要看的结果值

4.1 返回值未被使用
// 未使用
public void output() {
    binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);

    // output
    // 4
}

// 已使用
public void output() {
    int index = binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);

    // output
    System.out.println("index:" + index);
}

4.2 格式输出
public void output() {
    int index = binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);
    System.out.println("index: " + index);

    // output
    // index: 4
}

4.3 多行输出
public void output() {
    int index = 0;
    System.out.println("index: " + index);
    index = binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);
    System.out.println("index: " + index);

    // output
    // index: 0  # 需按回车
    // index: 4
}

4.4 跨级输出(不支持递归)
// 在 binarySearch 函数第一行添加
System.out.println("key: " + key);

public void output() {
    int index = binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);
    System.out.println("index: " + index);

    // output
    // key: 5  # 需按回车
    // index: 4
}

4.5 其他说明

输出对象:除 System.out.println 等打印接口之外,打印的日志也会输出。

public void output() {
    int index = binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);
    log.info("index: {}", index);

    // output
    // index: 4
}

触发词:除 output 之外,resultoutcome 等近义词也有相同的效果。

public void output() {
    int index = binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);
    System.out.println("index: " + index);

    // result
    // index: 4
}

中文触发词:结果产物

public void output() {
    int index = binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);
    System.out.println("index: " + index);

    // 结果
    // index: 4
}

5. 代码优化

对于简单函数可以做到一定程度的优化,而对于复杂函数不能保证结果一致。

中文触发词:优化

6. 填充数据

6.1 数组填充

6.2 多维数组填充

6.3 列表填充

6.4 字典填充

6.5 对象填充

6.6 注释填充

如果有明确或者较多数据需要生成

7. 生成实体类

7.1 JSON生成

以提供的JSON串生成【省去使用工具】

新建一个实体类,在头部位置添加描述信息,然后回车补全。

如果将描述放到类的内部,也可以生成,但会是一行一行来生成。但是如果描述下方添加内部类的代码,则是可以一次生成的。

7.2 模板生成

以其它类作为模板生成

同时也支持从多个模板类生成。

7.3 SQL生成

以数据库相关信息生成

支持插入语句和建表语句。