PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, December 20, 2022

[FIXED] How to use function types in interfaces in Go

 December 20, 2022     go, syntax     No comments   

Issue

I have the following code

type SomeInterface interface {
    SomeFunc(int, string)
}

type IntStringFunc func(int, string)

func (f IntStringFunc) SomeFunc(i int, s string) {
    f(i, s)
}

What is the syntax for invoking the method SomeFunc on the function type IntStringFunc?


Solution

The method calling syntax does not depend on the receiver type, it's always the same: value.methodName(params).

For example:

var f IntStringFunc = func(i int, s string) {
    fmt.Printf("f(%d, %s)\n", i, s)
}

f.SomeFunc(1, "one")

This will output (try it on the Go Playground):

f(1, one)

Since type of f is a function type, you can of course call it too like this:

f(1, "one")

Which of course will print the same.



Answered By - icza
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing