简介
无论你是零编程经验的新手,还是正在学习另一种编程语言,GitHub Copilot 都可以帮助你学习编程。 本指南专注于后一种使用场景:你已经掌握了一种或多种编程语言的编程知识,但现在希望学习一门新的语言。
先决条件
本指南假设你已了解如何在 IDE 中使用 Copilot 对话助手 和 Copilot 的代码补全功能。 请参阅“在 IDE 中向 GitHub Copilot 提问”和“使用 GitHub Copilot 在 IDE 中获取代码建议”。
先了解基础知识
使用 Copilot 对话助手 研究新语言的基础知识。 了解此语言与你已知道的语言有何不同。 向 Copilot 询问在开始使用新语言编程之前需要了解的主要事项。
示例提示:语言基础知识
以下是在 Copilot 对话助手 中可使用的一些提示,用于帮助你学习一门新编程语言的基础知识:
-
What is NEW-LANGUAGE best suited for? I am an experienced Python programmer, but I don't know anything about NEW-LANGUAGE.
-
What are the main ways in which NEW-LANGUAGE differs from other languages? Explain the most essential things I need to know as an experienced programmer who wants to learn to code in NEW-LANGUAGE.
-
How can I install NEW-LANGUAGE?
-
How does error handling work in NEW-LANGUAGE?
-
How do you manage dependencies and packages in NEW-LANGUAGE?
-
What are the most essential libraries or frameworks I should know about for NEW-LANGUAGE?
-
What testing frameworks does the NEW-LANGUAGE community typically use?
-
What are the biggest mistakes newcomers to NEW-LANGUAGE typically make with this language?
-
As an experienced programmer learning NEW-LANGUAGE, what are the things I should focus on first?
将 Copilot 当做私人培训师
GitHub Copilot 可为你编写代码。 可以让它创建代码块、函数甚至整个程序。 但在学习一门新语言时,你应避免过度依赖 Copilot 来替你编写大量代码,尤其是在未经理解的情况下直接接受它提供的代码建议。 如果你不熟悉这门语言,就无法判断 Copilot 提供的代码建议是否足够完善。 相反,应将 Copilot 视为你的个性化学习助手。
当让 Copilot 示范如何用新语言编写特定功能时,应当要求它解释所建议的代码逻辑。 如果你未能完全理解代码或 Copilot 的解释,可以要求它用更简单的方式说明(或者提供更详细的阐述),直到你确信自己已经完全掌握这段代码。 始终避免使用任何你未能完全理解的代码。
示例提示:请求解释
-
Explain your previous suggestion in more detail. I am new to NEW-LANGUAGE and I don't understand the code you suggested.
-
Show me how to write the following Ruby code in NEW-LANGUAGE: `people_over_50 = people.select { |person| person.age > 50 }`. Explain each part of the NEW-LANGUAGE code that you suggest.
-
Add comprehensive comments to this NEW-LANGUAGE file to explain what each part of the code does.
用新语言编写简单的程序
要开始,请用所熟悉的语言编写一个你能轻松完成的简单程序。 请求 Copilot 提供帮助。 如果你愿意,也可以让 Copilot 为你编写一个简单的程序,帮助你入门。 然后,你可以查看代码,了解它的工作原理,并在此基础上自行扩展程序。
示例提示:编写简单的程序
-
Show me the Rust code for a simple, useful command line tool that asks for user input and returns some useful information based on what was entered.
-
Comment the suggested code more thoroughly. I want to understand what every part of this code does.
-
Give me the code for a very small Android app written in Kotlin.
-
Suggests ways I could enhance this app.
使用注释和 Copilot 代码补全功能
作为另一种选择,你也可以在编辑器中写下注释,然后看看 Copilot 的代码补全功能会提供哪些建议,而不是直接请求 Copilot 对话助手 提供新程序的基础代码。
例如:
-
在 IDE 中,创建一个文件,并使用与你所使用的语言相对应的文件扩展名。 例如,如果要学习 Rust,请创建名为
example.rs
的文件。 -
复制并粘贴以下注释行。
Rust // NEW-LANGUAGE command line program to find the day of the week for a date. // The program does the following: // Prompt user to input string in format YYYYMMDD // Parse the string to check that it is a valid date. // If it's not, print an error message and exit. // Calculate the day of the week for the given date. // Print the day of the week to the user.
// NEW-LANGUAGE command line program to find the day of the week for a date. // The program does the following: // Prompt user to input string in format YYYYMMDD // Parse the string to check that it is a valid date. // If it's not, print an error message and exit. // Calculate the day of the week for the given date. // Print the day of the week to the user.
-
如有必要,请更改注释语法以匹配所使用的语言。
-
将
NEW-LANGUAGE
更改为所使用的语言的名称。 这一点,加上文件扩展名,将告知 Copilot 应使用哪种语言。 -
按下回车键和 Tab 键,即可查看并接受 Copilot 提供的代码补全建议。 继续接受建议,直到你拥有一个完整的程序为止。
-
通读代码以查看其工作原理。 如果你不理解代码的某个部分,可以请 Copilot 为你解释。 例如:
-
选择一行或多行你不理解的代码。
-
在 VS Code 中:****
右键单击所选代码并选择“Copilot” > “Explain”********。
在 JetBrains IDE 中:****
右键单击所选代码并选择“GitHub Copilot” > “Explain This”********。
在 Visual Studio 中:****
打开 Copilot 对话助手 面板并输入提示以下提示:
Explain this code
。
-
向 Copilot 提出具体问题
学习新语言时,应处理执行特定任务的小型代码单元。 向 Copilot 提出清晰且范围明确的问题,帮助你熟悉新语言的语法和惯用表达。
示例提示:具体问题
-
Explain all of the various ways that conditionals can be coded in NEW-LANGUAGE.
-
In JavaScript I'd write: `The ${numCats === 1 ? 'cat is' : 'cats are'} hungry.`. How do I write this in NEW-LANGUAGE?
-
In NEW-LANGUAGE, how do I convert a string to all lowercase?
-
What is the equivalent of num++ in NEW-LANGUAGE?
-
How do I run a program written in the NEW-LANGUAGE programming language?
-
How can I compile a single executable file for my NEW-LANGUAGE project, that I can distribute as a release?
将现有代码转换为新语言
利用你已有的编程知识的一个有效方法是,选取你熟悉的一种语言中的代码,将其转换为新语言的代码,然后对比并研究在新语言中如何实现相同的操作。
- 查找自包含的代码片段。 例如函数。
- 让 Copilot 对话助手 将其转换成你正在学习的语言。
- 将建议的代码复制并粘贴到编辑器中的新文件中。
- 并排查看这两段代码,分析如何用新语言完成相同的操作。 相似点和不同点有哪些呢?
- 让 Copilot 解释你不理解的任何代码部分。
阅读用新语言编写的现有代码
当你对新语言的基础知识感到熟悉后,可以花时间阅读用该语言编写的现有代码。
查找使用新语言的项目并查看代码。 打开一个文件,然后让 Copilot 对话助手 简要说明该文件的用途。 逐行阅读代码。 你是否理解代码中使用的技术? 你是否了解这些库和内置函数的工作原理? 是否可以通过代码跟踪数据流?
让 Copilot 为你解释你不理解的任何代码部分。
读完代码后,让 Copilot 提出改进代码的建议。
不要盲目认为 Copilot 总是正确的
Copilot 只是一个帮助你学习新语言的工具,它和所有 AI 助手一样,并非万能。 它可能会出错,也可能会建议一些并不最优的代码。
Copilot 是基于大量代码训练的,但对于每种语言,你收到的建议质量可能取决于该语言的训练数据的数量和多样性。 例如,JavaScript 在公共仓库中拥有丰富的资源,因此 Copilot 通常能够提供准确且有帮助的建议。 对于在公共仓库中较少见的语言,Copilot 提供的建议质量可能会较低。
请始终检查 Copilot 提供的代码,并确保在使用前你已经理解它。 检查 Copilot 提供的代码时,应寻找可以让代码更高效、更简洁或更易维护的方法。
如果你认为 Copilot 没有提供最优的实现方式,可以让它尝试用不同的方法来编写。
如果运行代码时出现错误,请将错误详情提供给 Copilot,并让它帮你修复代码。
还应检查 Copilot 生成的代码是否符合你的编码风格指南。 如果不符合,你可以修改仓库中的自定义指令,提示 Copilot 在以后遵守你的指南。 请参阅“为 GitHub Copilot 添加存储库自定义说明”。
示例提示:检查你的代码成果
-
Check this code for syntax errors.
-
Assess whether this code is optimally performant.
-
Suggest alternative ways this code could have been written.