Syntax error: unexpected NewLog, expecting (

hellow , im new to study golang. english is not very well. and for this code whats wrong with it? i cant find out. package mylogger
import (
“fmt”
“time”
)

type LogLevel uint16

const (
UNKNOWN LogLevel = iota
DEBUG
TRACE
INFO
WARNING
ERROR
FATAL
)

type Logger struct {
Level LogLevel

}

func parseLogLevel(s string) (LogLevel, error) {
s = strings.ToLower(s)
switch s {
case “debug”:
return DEBUGn, nil
case “trace”:
return TRACE, nil
case “info”:
return INFO, nil
case “warning”:
return WARNING, nil
case “error”:
return ERROR, nil
case “fatal”:
return FATAL, nil
default:
err := errors.New(“useless loglevel”)
return UNKNOWN, err
}

func NewLog (levelStr string) Logger {
level, err := parseLogLevel(levelStr)
if err != nil {
panic(err)
}
return Logger {
Level: level,
}
}

func (l Logger) enable(logLevel LogLevel) bool {
return l.Level >
}

func (l Logger) Debug (msg string) {
if i.enable(DEBUG) {
now := time.Now()
fmt.Printf("[%s] [DEBUG] %s\n", now.Format(“2006-01-02 15:04:05”), msg)
}
}

func (l Logger) Info (msg string) {
if l.enable(INFO) {
now := time.Now()
fmt.Printf("[%s] [INFO] %s\n", now.Format(“2006-01-02 15:04:05”), msg)
}
}

func (l Logger) Warning (msg string) {
if l.enable(WARNING) {
now := time.Now()
fmt.Printf("[%s] [WARNING] %s\n", now.Format(“2006-01-02 15:04:05”), msg)
}
}

func (l Logger) Error (msg string) {
if l.enable(ERROR) {
now := time.Now()
fmt.Printf("[%s] [ERROR] %s\n", now.Format(“2006-01-02 15:04:05”), msg)
}
}

func (l Logger) Fatal (msg string) {
if l.enable(FATAL) {
now := time.Now()
fmt.Printf("[%s] [FATAL] %s\n", now.Format(“2006-01-02 15:04:05”), msg)
}
}`

45:6: syntax error: unexpected NewLog, expecting (
56:33: syntax error: unexpected LogLevel, expecting comma or )

thanks!

Hi @Showbestt, welcome to the forum!

Please help us with helping you. The code that you posted is rather long, and it is not properly formatted.

Tip: These two simple steps help making code readable in this forum:

  1. Ensure your code is formatted by gofmt. Most Go-aware editors do this automatically when saving a Go file.

  2. When posting code here, place it between code fences, like so:

    ```go
    package main
    // ...more code...
    ```

The code will then look like this:

package main
// ...more code...

A code fence is a line with three backticks, and you can optionally pass the name of a file format (go, html, sh, json,…) to add syntax highlighting.

To troubleshoot the error you get, look at the lines before the line where the error occurs. A common reason for this error is a forgotten parenthesis, bracket, or curly brace.

In particular, it looks like the function parseLogLevel has no closing brace.

oh sorry, i will post it by the right way. And thanks for you help, im so stupid that i didnt see it. Thank you really!

No worries, this happens to everyone of us once in a while :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.