pakuula

Не импортируются пакеты Golang

Apr 18th, 2025 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. Текстовый архив с файлами к ответу на вопрос https://ru.stackoverflow.com/questions/1610502/
  2.  
  3. Скрипт для извлечения файлов из текстового архива
  4. """
  5. Скрипт извлекает файлы из текстового архива.
  6.  
  7. python script.py <путь_к_архиву>
  8. """
  9.  
  10. import logging
  11. import os
  12. import sys
  13.  
  14.  
  15. def extract_files(archive_file):
  16. filename = None
  17. chunk = []
  18. with open(archive_file, "rb") as file:
  19. for line in file:
  20. if line.startswith(b"--- "):
  21. if filename:
  22. body = b"".join(chunk[:-1])
  23. os.makedirs(os.path.dirname(filename), exist_ok=True)
  24. with open(filename, "wb") as output_file:
  25. output_file.write(body)
  26. logging.info(f"Извлечен файл: {filename}")
  27. filename = line[4:].strip()
  28. chunk = []
  29. elif filename is not None:
  30. chunk.append(line)
  31.  
  32.  
  33. if __name__ == "__main__":
  34. for file in sys.argv[1:]:
  35. extract_files(file)
  36.  
  37.  
  38. --- ./go.mod
  39. module statistic
  40.  
  41. go 1.23.6
  42.  
  43. --- ./internal/config/config.go
  44. package config
  45.  
  46. type Config struct {
  47. Postgres string
  48. }
  49.  
  50. func MustLoadByPath(path string) Config {
  51. return Config{}
  52.  
  53. }
  54.  
  55. --- ./internal/postgres/postgres.go
  56. package postgres
  57.  
  58. import (
  59. "context"
  60. "log/slog"
  61. )
  62.  
  63. type Pool struct{}
  64.  
  65. func (*Pool) Ping(context.Context) error {
  66. return nil
  67. }
  68.  
  69. type Db struct {
  70. Pool *Pool
  71. }
  72.  
  73. func NewStorageWithPool(ctx context.Context, postgres string, log *slog.Logger) (Db, error) {
  74. return Db{&Pool{}}, nil
  75. }
  76.  
  77. --- ./cmd/main.go
  78. package main
  79.  
  80. import (
  81. "context"
  82. "log/slog"
  83. "os"
  84. "statistic/internal/config"
  85. "statistic/internal/postgres"
  86. )
  87.  
  88. func main() {
  89. log := slog.New(
  90. slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
  91. )
  92.  
  93. cfg := config.MustLoadByPath("./config/config.yaml")
  94. ctx := context.Background()
  95.  
  96. db, err := postgres.NewStorageWithPool(ctx, cfg.Postgres, log)
  97. if err != nil {
  98. log.Error("failed init db " + err.Error())
  99. return
  100. }
  101.  
  102. if err = db.Pool.Ping(ctx); err == nil {
  103. log.Info("db connected suc")
  104. } else {
  105. log.Info("db connected failed " + err.Error())
  106. }
  107. }
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment