Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:40:06+00:00 2026-05-30T12:40:06+00:00

I wanted to create a function of a certain type. I’ve found one way

  • 0

I wanted to create a function of a certain type. I’ve found one way to do it, but there must be other, cleaner and nicer ways that do not include using var. What are the alternative ways to declare the function english of type Greeting?

package main

import "fmt"

type Greeting func(name string) string

func (g Greeting) exclamation(name string) string {
    return g(name) + "!"
}

var english = Greeting(func(name string) string {
    return "Hello, " + name
})

func main() {
    fmt.Println(english("ANisus"))
    fmt.Println(english.exclamation("ANisus"))  
}

In the example above, I can’t exchange var english = Greeting... with english := Greeting..., nor can I remove the Greeting(func ...) and just have the func stand alone since then I won’t be able to access the exclamation method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:40:08+00:00Added an answer on May 30, 2026 at 12:40 pm

    If mentioning var is your main problem, you can drop it easily, by changing = into :=, like this:

    english := Greeting(func(name string) string {
        return ("Hello, " + name);
    })
    

    But you don’t even have to cast your function into Greeting. The spec says this about function types:

    A function type denotes the set of all functions with the same parameter and result types.

    And this about type identity:

    Two function types are identical if they have the same number of parameters and result values, corresponding parameter and result types are identical, and either both functions are variadic or neither is. Parameter and result names are not required to match.

    This means that each function has its own function type. If two functions have the same signature (parameter and result types), they share one function type. By writing type Greeting func... you’re just giving a name to a particular function type, not defining a new one.

    So the following code works, and I hope shows the right way to work with function types in Go:

    package main
    
    import "fmt"
    
    type Greeting func(name string) string
    
    func say(g Greeting, n string) { fmt.Println(g(n)) }
    
    func french(name string) string { return "Bonjour, " + name }
    
    func main() {
            english := func(name string) string { return "Hello, " + name }
    
            say(english, "ANisus")
            say(french, "ANisus")
    }
    

    Notice that I also dropped semicolon and parenthesis from your english function. Go developers don’t use these punctuations if they don’t have to.

    UPDATE: Now that you’ve provided a sample code I can clearly understand the problem.

    For this purpose your code is good enough and there are not much other ways of doing it. If you like you can cast just before calling the method:

    english := func(name string) string { return "Hello, " + name }
    Greeting(english).exclamation("ANisus")
    

    But I’m not sure this is an improvement. I’m just saying that for what you want to do there does not seem to be other ways to write the code.

    That is, if we don’t want to change your types. I mean, the whole idea of calling a method on a function type seems a little weird. Not that it’s wrong, but a little rare. Another way of achieving the same effect in a more usual way is through a struct type and having a field for the function. Something like this:

    package main
    
    import "fmt"
    
    type Greeting struct {
        say func(name string) string
    }
    
    func newGreeting(f func(string) string) *Greeting {
        return &Greeting{say: f}
    }
    
    func (g *Greeting) exclamation(name string) string { return g.say(name) + "!" }
    
    func main() {
        english := &Greeting{say: func(name string) string {
            return "Hello, " + name
        }}
    
        french := newGreeting(func(name string) string {
            return "Bonjour, " + name
        })
    
        fmt.Println(english.exclamation("ANisus"))
        fmt.Println(french.exclamation("ANisus"))
    }
    

    Here english and french show two different ways of coding the same thing. Again, I’m not saying that this is the better solution, but a more usual and more flexible way of achieving the same effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to run a certain function as a thread, but I get SyntaxError:
I wanted to create a user registration page but it found out a difficult.
I wanted to create smth similar to this one Kansas county map where user
I wanted to create one js file which includes every js files to attach
I wanted to create some subdirectories inside my blob. But it is not working
I wanted to create a function which would return the path of the current
Say you had a timestamp function and then wanted to create a new function
I wanted to create a drop down at a certain position based on a
Usually if I wanted to create a page /user/profile i would create the function
I wanted to create search engine for my webpage, but during indexing on server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.