banner
amtoaer

晓风残月

竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。
github
x
telegram
steam
nintendo switch
email

Visual Studio Code Golang Problem-solving Guide

Irrelevant Chatter

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

Recently, I plan to get rid of my current Z4 extreme space and build a NAS with better performance. I will also provide an x86 remote development environment for ARM Mac. I might 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 using vscode, I found that there were several issues with the previous directory structure:

  1. The folders were in Chinese, which caused go mod to not recognize them (the reason the unit tests were working before was because Goland would copy the tests to a temporary directory with only English characters).
  2. Many old problem descriptions did not include the built-in data types, 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. This remake is mainly 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 root directory of the project.
     "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. Therefore, to achieve the effect of using an English path, the problem translation 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 a few screenshots of 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 work 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.