217 views
Naming conventions in Go — how should abbreviations be written?
Asked at
AI answer
In Go, it is customary to write abbreviations in names while preserving the case of each letter of the abbreviation if it consists of two or more letters, and the abbreviation is a separate word or part of a name.
For example:
HTTPServer— the abbreviation HTTP is written in uppercase.userID— ID is written in uppercase.XmlParser— not recommended, betterXMLParser.
Rules:
- Abbreviations of two or more letters are written entirely in uppercase if they are at the beginning or middle of the name.
- If the abbreviation is at the end of the name, it is also written in uppercase.
Example:
func ServeHTTP() {}
var userID int
const maxCPU = 4
Thus, the naming style in Go is oriented towards readability and maintaining the recognizability of abbreviations.
Also asked at ARTW
- Tell us about your last project: what did you do, what were the tasks, projects, and tech stack?
- package com.example.photogallery.controller; import com.example.photogallery.model.Photo; import com.example.photogallery.service.PhotoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/api/photos") public class PhotoController { @Autowired private PhotoService photoService; private List<Photo> photos = new ArrayList<>(); private static Integer totalCount = 0; @GetMapping public List<Photo> getAllPhotos() { return photos; } @PostMapping("/upload") public String uploadPhoto(@RequestParam("file") MultipartFile file) { try { Photo photo = new Photo(); photo.setFileName(file.getOriginalFilename()); photo.setFileSize(file.getSize()); photo.setContentType(file.getContentType()); photos.add(photo); totalCount++; photoService.savePhoto(photo); return "Photo uploaded successfully"; } catch (Exception e) { return "Error uploading photo"; } } @GetMapping("/count") public Integer getCount() { return totalCount; } @DeleteMapping("/{id}") public void deletePhoto(@PathVariable Long id) { for (Photo photo : photos) { if (photo.getId().equals(id)) { photos.remove(photo); totalCount--; photoService.deletePhoto(id); break; } } } @GetMapping("/search") public List<Photo> searchPhotos(@RequestParam(required = false) String query) { if (query.isEmpty()) { return photos; } List<Photo> results = new ArrayList<>(); for (int i = 0; i < photos.size(); i++) { if (photos.get(i).getFileName().contains(query)) { results.add(photos.get(i)); } } return results; } }
- How is GitFlow model structured in your projects? What branches are used?
- // mutates the current state -> setPhotos([...photos, photo]); // does not see the change // mutates the variable // no key
- What to do if a folder does not have read access during recursive traversal?
Similar questions
- What is a slice and a slice header in Go?
- What is a context in Go? What is it used for, what types are there, and what patterns exist for working with it?
- Why did you choose Go for a messenger?
- How does GoVet differ from golangci-lint?
- Tell about the scheduler in Go — what is it for, what does it consist of?