Golang задачи с собеседований
4.54K subscribers
489 photos
6 videos
12 files
241 links
Задачи, тесты и теоретические вопросы по Go. Так же по gin, grpc, горутинах, архитектуре api и http стэку.

Прислать задачу/вопрос в дар: @cyberJohnny
Сотрудничество: @cyberJohnny
加入频道
👨🏻‍💻 Вопрос от пользователя

spf13/viper не может прочитать файл

По аналогии с https://github.com/gogjango/gjango/blob/master/config/config.go

package config

import (
"fmt"
"log"
"os"
"path/filepath"
"read-clickhouse/tools"
"runtime"

"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)

func check() {
_, filePath, _, _ := runtime.Caller(0)
configName := "config.dev.yaml"
configPath := filePath[:len(filePath)-9] + "files" + string(filepath.Separator)

filename := fmt.Sprintf("%s%s", configPath, configName)

if _, err := os.Stat(filename); err == nil {
fmt.Println("Файл существует")
} else if os.IsNotExist(err) {
fmt.Println("Файл не существует")
} else {
fmt.Println("Ошибка при проверке файла:", err)
}
}

// Load returns Configuration struct
func Load(env string) *Configuration {
check()
_, filePath, _, _ := runtime.Caller(0)
configName := "config." + env + ".yaml"
configPath := filePath[:len(filePath)-9] + "files" + string(filepath.Separator)

tools.DebugPrintf(configName)
viper.SetConfigName(configName)
viper.AddConfigPath(configPath)

err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}

var config Configuration
viper.Unmarshal(&config)
setGinMode(config.Server.Mode)

return &config
}

// Configuration holds data necessery for configuring application
type Configuration struct {
Server *Server `yaml:"server"`
}

// Server holds data necessary for server configuration
type Server struct {
Mode string `yaml:"mode"`
}

func setGinMode(mode string) {
switch mode {
case "release":
gin.SetMode(gin.ReleaseMode)
break
case "test":
gin.SetMode(gin.TestMode)
break
default:
gin.SetMode(gin.DebugMode)
}
}

вывод

go run main.go
Файл существует
[/home/des/proj/go-read-clickhouse/config/config.go:read-clickhouse/config.Load 38]
%!(EXTRA string=config.dev.yaml)

2024/02/02 16:34:51 Config File "config.dev.yaml" Not Found in "[/home/des/proj/go-read-clickhouse/config/files]"
exit status 1
make: *** [Makefile:2: run] Ошибка 1

сам файл в папке есть. ось - убунта. права 0775 накинул


Golang задачи с собеседований
What does the following program print?
Anonymous Quiz
39%
[7 0 5 6]
61%
[7 6 5]
🤮19👍2🤔1
What does the following program print?
Anonymous Quiz
40%
3 3
14%
3 4
7%
0 0
39%
0 1
What does the following program print?
Anonymous Quiz
42%
321
17%
21
40%
1
What does the following program print?
Anonymous Quiz
22%
012 789
31%
012 987
31%
222 999
16%
222 789
👎12👍2
What does the following program print?
Anonymous Quiz
14%
77
20%
79
43%
99
24%
97
What does the following program print?
Anonymous Quiz
25%
999 999
57%
555 999
10%
999 555
8%
555 555