警告:去新手。我有这样的功能:
package main
import "flag"
import "fmt"
func print_out_type(x anything) string {
switch v := x.(type) {
case string:
return "A string"
case int32:
return "An Integer"
default:
return "A default"
}
}
func main() {
wordPtr := flag.String("argument1", "foo", "a String")
numPtr := flag.Int("argument2", 42, "an Integer")
flag.Parse()
fmt.Println("word: ", *wordPtr)
fmt.Println("number: ", *numPtr)
}
我试图根据类型返回不同类型的字符串。我只是陷入了如何编写一个接受不同类型参数的函数的问题。任何帮助表示赞赏。
答案 0 :(得分:14)
您可以使用接口类型作为参数,在这种情况下,您可以使用任何实现给定接口的类型调用该函数。在Go类型中,如果它们具有接口的方法,则自动实现任何接口。因此,如果要接受所有可能的类型,可以使用空接口(func print_out_type(x interface{}) string {
switch v := x.(type) {
case string:
return "A string"
case int32:
return "An Integer"
default:
return "A default"
}
}
),因为所有类型都实现了这一点。不需要对您的功能进行其他修改。
func print_out_type(x interface{}) string {
return reflect.TypeOf(x).String()
}
func main() {
fmt.Println(print_out_type(42))
fmt.Println(print_out_type("foo"))
}
您还可以使用反射包来研究接口变量的类型。例如:
public class StringToFormula
{
private string[] _operators = { "-", "+", "/", "*","^"};
private Func<double, double, double>[] _operations = {
(a1, a2) => a1 - a2,
(a1, a2) => a1 + a2,
(a1, a2) => a1 / a2,
(a1, a2) => a1 * a2,
(a1, a2) => Math.Pow(a1, a2)
};
public double Eval(string expression)
{
List<string> tokens = getTokens(expression);
Stack<double> operandStack = new Stack<double>();
Stack<string> operatorStack = new Stack<string>();
int tokenIndex = 0;
while (tokenIndex < tokens.Count) {
string token = tokens[tokenIndex];
if (token == "(") {
string subExpr = getSubExpression(tokens, ref tokenIndex);
operandStack.Push(Eval(subExpr));
continue;
}
if (token == ")") {
throw new ArgumentException("Mis-matched parentheses in expression");
}
//If this is an operator
if (Array.IndexOf(_operators, token) >= 0) {
while (operatorStack.Count > 0 && Array.IndexOf(_operators, token) < Array.IndexOf(_operators, operatorStack.Peek())) {
string op = operatorStack.Pop();
double arg2 = operandStack.Pop();
double arg1 = operandStack.Pop();
operandStack.Push(_operations[Array.IndexOf(_operators, op)](arg1, arg2));
}
operatorStack.Push(token);
} else {
operandStack.Push(double.Parse(token));
}
tokenIndex += 1;
}
while (operatorStack.Count > 0) {
string op = operatorStack.Pop();
double arg2 = operandStack.Pop();
double arg1 = operandStack.Pop();
operandStack.Push(_operations[Array.IndexOf(_operators, op)](arg1, arg2));
}
return operandStack.Pop();
}
private string getSubExpression(List<string> tokens, ref int index)
{
StringBuilder subExpr = new StringBuilder();
int parenlevels = 1;
index += 1;
while (index < tokens.Count && parenlevels > 0) {
string token = tokens[index];
if (tokens[index] == "(") {
parenlevels += 1;
}
if (tokens[index] == ")") {
parenlevels -= 1;
}
if (parenlevels > 0) {
subExpr.Append(token);
}
index += 1;
}
if ((parenlevels > 0)) {
throw new ArgumentException("Mis-matched parentheses in expression");
}
return subExpr.ToString();
}
private List<string> getTokens(string expression)
{
string operators = "()^*/+-";
List<string> tokens = new List<string>();
StringBuilder sb = new StringBuilder();
foreach (char c in expression.Replace(" ", string.Empty)) {
if (operators.IndexOf(c) >= 0) {
if ((sb.Length > 0)) {
tokens.Add(sb.ToString());
sb.Length = 0;
}
tokens.Add(c);
} else {
sb.Append(c);
}
}
if ((sb.Length > 0)) {
tokens.Add(sb.ToString());
}
return tokens;
}
}
将打印
int
的字符串
答案 1 :(得分:-3)
eg: value, ok := temp.(int)
interface{}
类型变量作为函数的参数。在函数中,您可以使用类型断言来检查参数的实际类型并打印出来。