Function names can be same as built in?

 package log

import (
	"context"
	"log"
	"math/rand"
	"net/http"
)

const requestIDKey = 42

func Println(ctx context.Context, msg string) {
	id, ok := ctx.Value(requestIDKey).(int64)
	if !ok {
		log.Println("could not find request ID in context")
		return
	}
	log.Printf("[%d] %s", id, msg)
}

func Decorate(f http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		id := rand.Int63()
		ctx = context.WithValue(ctx, requestIDKey, id)
		f(w, r.WithContext(ctx))
	}
}

Hi, I saw this code in this video: https://www.youtube.com/watch?v=LSzR0VEraWw

and I don’t understand how he can use Println and Decorate as a custom function name.
I am getting yellow underlines which says “exported function Println should have comment or be unexported”

I don’t understand what it means :frowning:

exported function Println should have comment or be unexported

means that either you should add a comment above your function to say what it does, like

// Println prints lines to console
func Println(...) {}

Or you should not have the function exported. Any function starting with a capital letter is exported.
This means the function can be used from outside your package.

2 Likes

How outside the package? Does it become like when we go install something, and it can be called in any .Go file?

package main

import "example.com/your/package"

func main() {
  package.Println(...)
}

As any other function you call from an external package.

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