amtoaer

晓风残月

叹息似的渺茫,你仍要保存着那真!
github
x
telegram
steam
nintendo switch
email

Visual Studio Code Golang Problem-solving Guide

Random Thoughts#

I haven't updated my blog for a long time. I'll just write an article using the readme I wrote after remaking the problem-solving repository!

Recently, I plan to get rid of my current Z4 workspace and build a NAS with better performance myself, while also providing an x86 remote development environment for the ARM-based Mac. I might possibly update a series of articles describing my process of tinkering in the near future, so stay tuned!~

Main Text#

Recently, I had the urge to do some problem-solving. When I opened the problem-solving repository with vscode, I found that there were quite a few issues with the previous directory structure:

  1. The folders were in Chinese, which caused go mod to not recognize them (the reason why the unit tests were working fine before was because Goland would copy the tests to a temporary directory with only English characters).
  2. Many of the old problem's built-in data types were not included, causing syntax errors even after renaming the folders and recognizing them correctly.

So I decided to let go of the past and remove all the previous code. The main purpose of this remake is to ensure that:

  1. When opening the problem-solving repository directory, all files can be recognized correctly.
  2. Each file has its own unit test that can be run in batches.

Implementation:

  1. Create a Golang project in the root directory of the project.
     go mod init github.com/amtoaer/leetcode
    
  2. Install the vscode-leetcode plugin by labuladong.
  3. Set the problem-solving language to Go and set the file save directory to the project's root directory.
     "leetcode.workspaceFolder": "/Users/amtoaer/Documents/code/go/go-leetcode",
     "leetcode.defaultLanguage": "golang",
    
  4. Set the specific path for file saving (in English without spaces) and the file template with unit tests.

Note:
The existing LeetCode extension does not support using the English name of the problem as a variable, so in order to achieve the effect of using an English path, the translation of the problem needs to be turned off.

 "leetcode.useEndpointTranslation": false,
 "leetcode.filePath": {
     "default": {
     "folder": "${id}.${camelCaseName}",
     "filename": "solution_test.${ext}"
     }
 },
 "leetcode.codeTemplate": "package main\n\nimport (\n\t\"testing\"\n)\n\n${code}\n\nfunc Test(t *testing.T) {\n}",
  1. Gofmt does not limit line length, so there may be lines of code that are too long. Use golines for code formatting instead.
     "editor.formatOnSave": true,
     "go.alternateTools": {
         "customFormatter": "/Users/amtoaer/.go/bin/golines"
     },
     "go.formatTool": "custom"
    

Here are some screenshots of the problem-solving after configuration:

  1. Problem-solving in the editor
    Xnip2023-07-02_10-43-51

  2. Normal code completion
    Xnip2023-07-02_10-53-29

  3. Running unit tests
    Xnip2023-07-02_10-45-23

  4. Running unit tests in batches
    Xnip2023-07-02_10-46-15


What? You're asking if I wrote the test cases myself?

Of course not! Copilot will do that job for you. Just type assert.Equal, press tab a few times, and you'll have all the test cases!

Xnip2023-07-02_10-58-00

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.